我正在使用jQuery Ajax调用来查询返回普通JSON数据的跨域URL:
function getAlibrisSuggestions(theKeywords)
{
$.ajax(
{
url: "http://www.alibris.com/cgi-bin/complete?site=18640838&mtype=B&term=" + encodeURI(theKeywords),
dataType: 'jsonp',
type: "GET",
crossDomain: true,
/* None of these helped obviously since the return is not JSONP but JSON
complete: onSuggestionsFetched,
beforeSend: onBeforeSend,
success: onSuccess,
dataFilter: onDataFilter,
*/
});
}
示例网址请求:
http://www.alibris.com/cgi-bin/complete?site=18640838&mtype=B&term=book
JSON回复:
[
{"l": "BP", "v": "books llc"},
{"l": "BA", "v": "books time-life"},
{"l": "BA", "v": "books general"},
{"l": "BA", "v": "books sunset"},
{"l": "BP", "v": "books on demand"},
{"l": "BA", "v": "books golden"},
{"l": "BA", "v": "books scholastic"},
{"l": "BP", "v": "book jungle"},
{"l": "BA", "v": "books chronicle"},
{"l": "BP", "v": "booksurge publishing"}
]
由于它是跨域的,我必须使用JSONP来避免原始错误。问题是,正如您从该URL的响应中看到的那样,返回是纯JSON。由于它没有格式化为Javascript,因此它会导致jQuery在Chrome控制台中返回结果状态“parsererror”和预期的“资源解释为脚本但在MIME类型text / plain中传输”警告。
我无法控制外部服务器。我想避免将调用包装在服务器端脚本中,只是为了将返回装扮为Javascript。有谁知道能够处理回报的聪明方法吗?
- roschler
答案 0 :(得分:1)
不可能,抱歉。在您的服务器上创建一个转发请求的页面是您最好的选择。