更新1:通过jQuery获取JSONP
这是我所得到的在浏览器中,如果我键入
http://www.remote_host.com/feed.php?callback=jsonpCallBack
{
"rss": {
"channels": [
{
"title": "title goes here",
"link": "http://www.remote_server.com/feed.php",
"description": "description goes here",
"items": [
{
"title": "item title goes here",
"link": "item link goes here",
"pubDate": "item date goes here",
"description": "item description goes here"
},
{
"title": "item title goes here",
"link": "item link goes here",
"pubDate": "item date goes here",
"description": "item description goes here"
},
{
"title": "item title goes here",
"link": "item link goes here",
"pubDate": "item date goes here",
"description": "item description goes here"
}
]
}
]
}
}
所以这不是JSONP?
原题:
我有以下脚本,我试图从远程主机获得JSON数据:
$(document).ready(function() {
get_json_feed();
function get_json_feed() {
$.ajax({
url: 'http://www.remote_host.com/feed.php?type=json',
type: 'GET',
dataType: 'jsonp',
error: function(xhr, status, error) {
alert("error");
},
success: function(json) {
alert("success");
}
});
}
});
但由于某些原因,我得到一个错误和警告:
Warning: Resource interpreted as Script but transferred with MIME type text/html.
Error: Uncaught SyntaxError: Unexpected token :
我在做什么错?
没有ü尝试改变类型? – diEcho 2011-06-02 11:23:32
@diEcho,哪种类型?我在客户端类型和数据类型有两种类型,我在服务器端有一个类型。 – oshirowanen 2011-06-02 11:24:51
这听起来像服务器不返回JSONP。你应该尝试(a)'feed.php?type = jsonp'和(b)如果服务器支持JSONP,它通常会接受一个参数来指定回调名称,如:'feed.php?type = jsonp&callback = ?'。您必须阅读您正在使用的服务的文档。 – 2011-06-02 11:24:55