我刚刚了解到,将 getJson 与来自另一个域/端口的 url 一起使用通常会导致跨域策略问题。
使用此代码:
var appGetApi = "http://localhost:30028/api/values";
$.getJSON(appGetApi, function (_returnedJson) {
...
});
我收到此错误:
XMLHttpRequest cannot load http://localhost:30028/api/values.
Origin http://localhost:17437 is not allowed by Access-Control-Allow-Origin.
在网上搜索答案后,似乎添加了&callback=? 是一个著名的修复。所以我就这么做了。
var appGetApi = "http://localhost:30028/api/values&callback=?";
$.getJSON(appGetApi, function (_returnedJson) {
...
});
但我仍然得到一个错误:
Failed to load resource: the server responded with a status of 400 (Bad Request)
http://localhost:30028/api/values&callback=jQuery11020629610788077116_1373178114158?_=1373178114159
这是我第一次使用 API,我现在完全不知道如何解决这个问题。请帮帮我。谢谢。