![](/img/trans.png)
[英]Angular $http.json promise returning error, though I can see the response
[英]Calling a PHP returning a JSON returns an error though I can see the response
這是我用來對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。 我特別在瀏覽器控制台中遇到的錯誤是:
未捕獲到的SyntaxError:意外令牌:index.php?callback = jQuery32103297264247416809_1516181997373&country = uk&_ = 1516181997374:1
您指示jQuery請求JSONP並自動對其進行解碼:
dataType: 'jsonp',
然后,您采用jQuery的解碼數據並將其作為JSON處理,這不是過去,也從未如此:
success: function (response){
var result = $.parseJSON(response);
console.log(result);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.