问题描述
|
我正在使用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\"},\"v\": \"books general\"},\"v\": \"books sunset\"},{\"l\": \"BP\",\"v\": \"books on demand\"},\"v\": \"books golden\"},\"v\": \"books scholastic\"},\"v\": \"book jungle\"},\"v\": \"books chronicle\"},\"v\": \"booksurge publishing\"}
]
由于它是跨域的,因此我必须使用JSONP以避免起源错误。正如您从该URL的响应中看到的那样,问题是返回的内容是纯JSON。由于未将其格式化为Javascript,因此会导致jQuery返回结果状态“ parsererror”和预期的“资源被解释为脚本但已传输为MIME类型文本/纯文本”警告,该警告在Chrome \的控制台中。
我无法控制外部服务器。我想避免将调用包装在服务器端脚本中,只是为了将返回装扮成Javascript。有人知道能够处理退货的聪明方法吗?
-罗施勒
解决方法
不可能,抱歉。在您的服务器上制作一个页面以转发请求是您的最佳选择。