【发布时间】:2017-10-14 06:54:40
【问题描述】:
我的脚本不会在 Select2 中加载任何数据。我用 JSON 数据制作了一个 test.php(一切正常后将在外部提供。(test.php 是我的内部测试))。
test.php 的输出
[{"suggestions": ["1200 Brussel","1200 Bruxelles","1200 Sint-Lambrechts-Woluwe","1200 Woluwe-Saint-Lambert"]}]
jQuery 脚本:
$("#billing_postcode_gemeente").select2({
minimumInputLength: 2,
tags: [],
ajax: {
url: 'https://www.vooronshuis.nl/wp-content/plugins/sp-zc-checkout/test.php',
dataType: 'json',
type: "GET",
quietMillis: 50,
data: function (data) {
alert(data);
},
processResults: function(data) {
return {
results: $.map(data.suggestions, function(obj) {
return {
id: obj.key, text: obj.value
}
})
};
}
}
});
我一直在搜索和检查所有其他解决方案。它对我不起作用。我被卡住了。
更新:到目前为止的 jQuery 脚本
$("#billing_postcode_gemeente").select2({
minimumInputLength: 2,
placeholder: "Voer uw postcode in..",
ajax: {
url: 'https://www.vooronshuis.nl/wp-content/plugins/sp-zc-checkout/checkaddressbe.php',
dataType: 'json',
type: "GET",
quietMillis: 50,
data: function (data) {
return {
ajax_call: 'addressZipcodeCheck_BE',
zipcode: '1200'
};
},
processResults: function(data) {
alert(data);
correctedData = JSON.parse(data)[0]suggestions;
alert(correctedData);
return {
results: $.map(correctedData, function(obj) {
return {
id: obj.key,
text: obj.value
}
})
};
}
}
});
【问题讨论】:
-
控制台有错误吗?内部测试是什么意思?
-
@SaadSuri 通过内部测试,我的意思是我已经设置了一个自己的 test.php 来回显 json 代码。就这样。现在我看到一个错误: SyntaxError: missing ; before 语句 - LINE: id: obj.key, text: obj.value 我之前还没有这个错误。知道如何解决这个问题吗?
-
更新:我添加了一个 return { } 。现在没有错误了。但如果我输入“12”,它仍然不会加载我的 JSON 数据。它应该显示我的数据。
-
你的json文件在不同的服务器上吗?
-
@SaadSuri:目前没有。它是相同的 URL。警报也显示以下内容:[object Object]