我已经设置了ajax
这就需要跨域,一些API工作没有任何错误,但有些回报parserror
。
我无法弄清楚为什么发生这种情况,以及如何解决它,希望任何人都可以给我一些方向,谢谢。跨域Ajax的失败 - parseerror回调不叫
这里是我的代码:
// After OAuth2 to get the token
$.ajaxSetup({
dataType: 'jsonp',
statusCode: {
401: function() {
window.open('page for oauth2');
}
}
});
$.ajax({
url: 'https://redbooth.com/api/3/me', // this one work without problem
method: 'GET'
})
.done(function(data, textStatus, jqXHR) {
alert('success');
});
$.ajax({
url: 'https://redbooth.com/api/3/projects?order=id&archived=false',
method: 'GET'
})
.done(function(data, textStatus, jqXHR) {
alert('test');
})
.fail(function(jqXHR, textStatus, errorThrown) {
// this API will throw error
// jqXHR: status = 200, readyState = 4, statusText = "load"
// textStatus: parserror
// errorThrown: xxx was not called
var test = 1;
});
我从他们的控制台提琴手检查返回值,也:
[{ “类型”: “计划”, “created_at”: 0 “的updated_at”:0, “ID”:0, “永久”: “测试1cc1c3”, “的organization_ID”:0, “存档”:假的, “名”: “测试”, “说明”:空, “起始日期”:空, “END_DATE”:空, “tracks_time”:假的, “公”:真实的, “publish_pages”:假, “设置”:{}, “USER_ID”:0, “删除”:假” last_goal_user_id“:0}]
请注意,我在这里修改的值,只需更换那些ID 0
我试图验证返回值在JSON验证或使用$.parseJSON('return value')
,既可以返回JSON对象没有问题。
我曾尝试以下操作:
- I am getting a json parse error on a cross-domain ajax call not sure how to get rid of the issue
- 设置'jsonp': false
- 设置'dataType': 'text'
,导致跨起源问题
- 设置'dataType':'text,'crossDomain':true
,仍然跨域问题
我试图避开它,如果它是可能的 – Prisoner
它很好的做法,以避免服务器的干预,但对于跨域的问题,我建议你去的服务器端触发。希望这可以帮助你。 –