更新1:
如果我在浏览器中键入以下内容,就会得到这样的结果。
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");
}
});
}
});
但是出现了错误和警告,原因不明:
我做错了什么?警告:资源被解释为脚本,但传输的MIME类型为text/html。
错误:未捕获的语法错误:意外的令牌“:”
feed.php?type=jsonp
和(b)如果服务器支持JSONP,它通常会接受一个参数,您可以指定回调函数的名称,类似于:feed.php?type=jsonp&callback=?
。您必须阅读您正在使用的服务的文档。 - Felix Klingjsonp: 'callback', jsonpCallback: 'jsonpCallback',
。 - xkeshav