JSON可以在Safari中加载,但不能在Firefox或Chrome中加载

JSON loads in Safari but not in Firefox or Chrome

本文关键字:加载 Firefox Chrome 但不能 Safari JSON      更新时间:2023-09-26

我试图在我正在使用javascript和worldweatheronline.com api构建的网站上显示天气。在Safari中测试时,我可以提取所需的所有数据,但在Firefox和Chrome中什么都没有显示。我使用了以下代码:

var url = 'http://api.worldweatheronline.com/free/v1/weather.ashx?q=Gent&'http://api.worldweatheronline.com/free/v1/weather.ashx?q=Gent&
jQuery.get(url,function(r){
                        document.getElementById("weather").innerHTML +=  .... ; // do something
                    },"JSON");

我在论坛和谷歌上寻找另一种获取json数据的方法,并提出了以下建议:

$.ajax({
                        url: 'http://api.worldweatheronline.com/free/v1/weather.ashx?q=Gent&format=json&cc=yes&key=pjzd2w42md9qacscthr9gw4h',
                        type: 'GET',
                        dataType: 'json',
                        success: function(r) {
                            alert("test");
                        },
                        error: function() {
                            alert("error");
                    }});

同样,Safari给出一个包含"test"的警告,但Firefox和Chrome都显示"error"。当我查看Web控制台时,我没有看到任何错误,只有HTTP/1.1 200 OK消息。我找了好几个小时,但还是找不到解决办法。如果有人看到我做错了什么,请告诉我。

BTW: JSONLint说url是有效的。

EDIT:尝试了dataType: 'json'和dataType: 'jasonp',但结果相同。

它可能会对您的查询字符串吠叫。我想把它设置得更合适一点:

$.ajax({
    type:'GET',
    url:'http://api.worldweatheronline.com/free/v1/weather.ashx',
    data:{q:'Gent',format:'json',cc:'yes',key:'pjzd2w42md9qacscthr9gw4h'},
    success:function(r){
        alert('success');
    },
    error:function(){
        alert('error');
    }
});

通过使您的data这样,它应该工作正常。至少它会更具可读性和可扩展性。

相关文章: