Javascript JSON:错误域
当我尝试使用PHP API中的信息时,请求中出现错误:Javascript JSON:错误域,javascript,jquery,ajax,json,jsonp,Javascript,Jquery,Ajax,Json,Jsonp,当我尝试使用PHP API中的信息时,请求中出现错误: OPTIONS http://api.reddrummer.com/summary.json?api_key=tok 405 (Method Not Allowed) jquery.js:130 OPTIONS http://api.reddrummer.com/summary.json?api_key=tok Invalid HTTP status code 405 jquery.js:130 XMLHttpRequest cannot
OPTIONS http://api.reddrummer.com/summary.json?api_key=tok 405 (Method Not Allowed) jquery.js:130
OPTIONS http://api.reddrummer.com/summary.json?api_key=tok Invalid HTTP status code 405 jquery.js:130
XMLHttpRequest cannot load http://api.reddrummer.com/summary.json?api_key=tok. Invalid HTTP status code 405
(tok是我从API访问信息的令牌)
这是我的ajax,因此我将数据类型更改为接收jsonp而不是json:
(function($){
document.addEventListener("deviceready", function(){
tok = window.sessionStorage.getItem("token");
jQuery.ajax({
type: 'GET',
url: 'http://api.reddrummer.com/summary.json?api_key=' + tok,
contentType: "application/json",
dataType: 'jsonp',
jsonpCallback: 'callback',
jsonp: false,
success: function(d) {
alert("ok");
},
error: function(error){
alert("not ok");
}
});
}, false);
})(jQuery);
现在还有另一个错误:
Uncaught SyntaxError: Unexpected token : summary.json:2
这是我的json中的一个错误,因此我已经验证了我的json,并且没有任何问题:
{
"username": "Guilherme",
"language": "pt-br",
"position": "Java Pleno",
"company_name": "Reddrummer",
"photo": "http://drumcircle.reddrummer.com/p/photo/user/guiandmag@gmail.com",
"post": {
"total_post": "1",
"total_comments": "1",
"groups": 1,
"groups_url": {
".Delivery Brasil": "http://drumcircle.reddrummer.com/#houseorgan/2566054"
},
"people": 2
},
"chart": {
"_video": 0,
"_image": 0,
"project": 0,
"topic": 0,
"note": 0,
"mobile": 0,
"task": 0,
"press": 0,
"bug": 0,
"reuniao": 1,
"sap": 0,
"oracle": 0,
"sharep": 0,
"vendas": 0,
"ideia": 0
}
}
我需要一些帮助,因为我正在尝试查看文档和许多其他网站,但当我尝试使用JSONP从API接收值时,错误总是一样的。JSONP使从其他域请求服务/数据成为可能。该格式与经典的JSON对象略有不同 您的服务需要将数据包装到回调函数中,并将其作为参数传递。此回调通常是动态的,并作为查询字符串参数传递(即jQuery通过附加“callback”参数键来自动执行) 假设jQuery$.ajax()调用的URL是
http://api.reddrummer.com/summary.json?api_key=12345&callback=jQuery152031713143712840974_1378285818229&_=1378285824291
,服务需要返回:
jQuery152031713143712840974_1378285818229(
{
"username": "Guilherme",
"language": "pt-br",
"position": "Java Pleno",
"company_name": "Reddrummer",
"photo": "http://drumcircle.reddrummer.com/p/photo/user/guiandmag@gmail.com",
"post": {
"total_post": "1",
"total_comments": "1",
"groups": 1,
"groups_url": {
".Delivery Brasil": "http://drumcircle.reddrummer.com/#houseorgan/2566054"
},
"people": 2
},
"chart": {
"_video": 0,
"_image": 0,
"project": 0,
"topic": 0,
"note": 0,
"mobile": 0,
"task": 0,
"press": 0,
"bug": 0,
"reuniao": 1,
"sap": 0,
"oracle": 0,
"sharep": 0,
"vendas": 0,
"ideia": 0
}
}
);
JSONP响应与JSON响应不同。您得到的是JSON,因此这应该是您的
数据类型。在我看来,你的服务器有问题。
jQuery152031713143712840974_1378285818229(
{
"username": "Guilherme",
"language": "pt-br",
"position": "Java Pleno",
"company_name": "Reddrummer",
"photo": "http://drumcircle.reddrummer.com/p/photo/user/guiandmag@gmail.com",
"post": {
"total_post": "1",
"total_comments": "1",
"groups": 1,
"groups_url": {
".Delivery Brasil": "http://drumcircle.reddrummer.com/#houseorgan/2566054"
},
"people": 2
},
"chart": {
"_video": 0,
"_image": 0,
"project": 0,
"topic": 0,
"note": 0,
"mobile": 0,
"task": 0,
"press": 0,
"bug": 0,
"reuniao": 1,
"sap": 0,
"oracle": 0,
"sharep": 0,
"vendas": 0,
"ideia": 0
}
}
);