跨域 Ajax 失败 - parseerror 作为未调用的回调
Cross Domain Ajax fail - parseerror as callback not called
我设置了一个需要跨域的ajax
,有些API可以正常工作,但有些return parserror
.
想不通为什么会这样,怎么解决,希望大家指点一下,谢谢。
这是我的代码:
// 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;
});
我已经从他们的控制台和 fiddler 中检查了 return 值:
[{"type":"Project","created_at":0,"updated_at":0,"id":0,"permalink":"test-1cc1c3","organization_id":0,"archived":false,"name":"test","description":null,"start_date":null,"end_date":null,"tracks_time":false,"public":true,"publish_pages":false,"settings":{},"user_id":0,"deleted":false,"last_goal_user_id":0}]
请注意我已经修改了这里的值,只需将那些id替换为0
我试图在 JSON 验证器 或使用 $.parseJSON('return value')
中验证 return 值,两者都可以 return 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
,仍然是跨源问题
如果 jsonp 不起作用,您可以尝试从服务器调用 url 而不是从 javascript/ajax.
调用
$.ajax({
url : 'proxy.php',
data: {url :'https://redbooth.com/api/3/projects?order=id&archived=false'},
method: 'GET'
})
.done(function( data, textStatus, jqXHR ) {
alert('test');
})
proxy.php
<?php
if(isset($_POST['url']) and !empty($_POST['url'])) {
$data = file_get_contents($_POST['url']);
print $data;
}
?>
我设置了一个需要跨域的ajax
,有些API可以正常工作,但有些return parserror
.
想不通为什么会这样,怎么解决,希望大家指点一下,谢谢。
这是我的代码:
// 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;
});
我已经从他们的控制台和 fiddler 中检查了 return 值:
[{"type":"Project","created_at":0,"updated_at":0,"id":0,"permalink":"test-1cc1c3","organization_id":0,"archived":false,"name":"test","description":null,"start_date":null,"end_date":null,"tracks_time":false,"public":true,"publish_pages":false,"settings":{},"user_id":0,"deleted":false,"last_goal_user_id":0}]
请注意我已经修改了这里的值,只需将那些id替换为0
我试图在 JSON 验证器 或使用 $.parseJSON('return value')
中验证 return 值,两者都可以 return 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
,仍然是跨源问题
如果 jsonp 不起作用,您可以尝试从服务器调用 url 而不是从 javascript/ajax.
调用$.ajax({
url : 'proxy.php',
data: {url :'https://redbooth.com/api/3/projects?order=id&archived=false'},
method: 'GET'
})
.done(function( data, textStatus, jqXHR ) {
alert('test');
})
proxy.php
<?php
if(isset($_POST['url']) and !empty($_POST['url'])) {
$data = file_get_contents($_POST['url']);
print $data;
}
?>