user2358985
user2358985

Reputation:

JSON loads in Safari but not in Firefox or Chrome

I'm trying to display the weather on a site I'm building using javascript and the worlweatheronline.com api. I could extract all the data I needed when testing in Safari, but in Firefox and Chrome nothing showed up. I was using the following code:

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");

I looked here on the forum and google for an alternative way to get the json data and came up with the following:

$.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");
                    }});

Again, Safari gives an alert containing "test" but both Firefox and Chrome display "error". When I look in the Web Console I see no errors, only a HTTP/1.1 200 OK message. I searched for hours but I can't find a solution... If somebody sees what I'm doing wrong, please let me know.

BTW: JSONLint says the url is valid.

EDIT: Tried both dataType: 'json' and dataType: 'jasonp' but same results.

Upvotes: 0

Views: 472

Answers (1)

PlantTheIdea
PlantTheIdea

Reputation: 16359

It is likely barking at your query string. I would set it up a little more properly:

$.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');
    }
});

By making your data as such, it should work alright. At least it'll be more readable and extensible.

Upvotes: 1

Related Questions