jquery ajax 响应处理程序在找不到以值为前缀的空格时似乎失败。此 ajax 回复失败
{"ip":"bla"}
这通过
{"ip": "bla"}
这是我正在尝试的代码的简化版本
<html lang="en">
<body>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$.ajax({
type: "GET",
url: "https://api.myjson.com/bins/4as0v",
dataType: 'jsonp',
contentType: "application/json",
success: function (data) {
alert('Success call back called')
}
})
</script>
Hi
</body>
</html>
下面是我收到的错误
Uncaught SyntaxError: Unexpected token :
现在,我尝试将 $.ajax 中的 dataType 更改为文本,并在回调中手动解析为 json 的文本,这有效。
dataType: 'jsonp',
。
success: function (data) {
data = JSON.parse(data);
}
但是,在我的实际应用程序中,我需要发出跨域请求,因此被迫使用 jsonp 数据类型。我还尝试了多个空格分隔的值(从 Jquery 1.5 开始)来尝试将 jsonp 解释为文本,但没有成功。
请注意,我打算从中访问数据的 url 的 ReponseHeader 没有将 Access-Control-Allow-Origin 属性设置为 *
任何提示/建议都深表感谢。
首先,您的代码请求的是 json url 而不是 jsonp。请阅读此内容以获取 jsonp http://en.wikipedia.org/wiki/JSONP
为了使您的示例正常工作,您可以使用 $.getJSON 来读取 json。
$.getJSON('https://api.myjson.com/bins/4as0v', function(data){
alert('Success call back called n'+data.ip)
});