【发布时间】:2017-05-04 12:15:42
【问题描述】:
这是我第一次来这里,请多多包涵。
我有这两个类似的 web api,都可以使用浏览器访问,但只有一个可以使用 jquery。什么给了?
这个可以用浏览器和getjson,都不错:
$.getJSON( "http://api.openweathermap.org/data/2.5/weather?q=london&appid=33d0ba5efa0edb1a2282c3165b00d15e&units=metric")
.done(function( json ) {
console.log( "JSON Data: " + json.name );
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
});
下面这个几乎是一样的,但是只有当你去地址时才可用,如果你尝试获取 json 它会静默失败或抛出错误:
$.getJSON( "http://info.studyinpoland.pl/admin/public/api/events")
.done(function( json ) {
console.log( "JSON Data: " + json[0].id );
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
});
为什么会失败?我需要来自http://info.studyinpoland.pl/admin/public/api/events 的数据,我还能如何使用 javascript 访问它?
【问题讨论】:
-
谢谢。问题确实是跨域的,它已更改为服务器端。
标签: javascript json web-services getjson