这是我用来对PHP进行AJAX调用的javascript:
$(document).ready(function(e) {
$(function(){
$.ajax({
type:'GET',
dataType: 'jsonp',
data: {
country: "uk"
},
url: 'http://api.mysite.uk/advertorial/index.php',
success: function (response){
var result = $.parseJSON(response);
console.log(result);
},
error : function () {
console.log('Error');
}
});
});
});
返回如下结构的JSON:
{"id":"1","name":"test","country":"uk","header":"Header","pre_cta_text":"Pre CTA","cta_text":"CTA text","cta":"CTA","img":null,"active":"1"}
即使调用出错,我也可以看到它返回上面的JSON。我特别在浏览器控制台中遇到的错误是:
Uncaught SyntaxError:意外的令牌: ?的index.php回调= jQuery32103297264247416809_1516181997373&安培;国家= UK&安培; _ = 1516181997374:1
答案 0 :(得分:1)
您指示jQuery请求JSONP并自动解码:
dataType: 'jsonp',
然后你获取jQuery的解码数据并将其作为JSON处理,这不是也从未出现过:
success: function (response){
var result = $.parseJSON(response);
console.log(result);
}