有效json的jQuery语法错误
我试图使用jQuery ajax获取instagram用户详细信息,可以从url找到
https://www.instagram.com/instagram/?__a=1
但是当我从下面的代码中通过ajax调用它时
$(document).ready(function(){ var instagram = 'instagram'; $.ajax({ url : 'https://www.instagram.com/'+ instagram +'/?__a=1', dataType: "jsonp", success: function(e) { document.write(JSON.stringify(e)); } }); });
它给我一个错误:
失踪 ; 在声明之前[在Json]
但是,当我复制并粘贴它以便在一些可用的在线工具上进行validation时,它显示有效格式化的json。
演示: https : //codepen.io/abhibagul/pen/WXGoYb? editors = 0010
浏览器控制台中显示错误。
如果您正在以JSON
,请从dataType: "jsonp"
更改为dataType: "json"
Valide json的回应看起来像
{ "Name": "Foo", "Id": 192.168.73.96, "Rank": 7 }
和jsonp会是一样的
parseResponse({"Name": "Foo", "Id": 1234, "Rank": 7});
在此处获取更多详细信息
Instagram不支持jsonp,它不再用于支持CORS。 将jsonp
更改为json
。 但是,这不起作用,因为Instagram的CORS策略阻止了它。 您需要使用Instagram提供的API。
$(document).ready(function() { var instagram = 'instagram'; $.ajax({ url: 'https://www.instagram.com/' + instagram + '/?__a=1', dataType: "json", success: function(e) { document.write(JSON.stringify(e)); } }); });