0

I've got the following problem: I need to download a JSON file from an API via JQuery / JavaScript. In theory this should be quite basic.

I tried $.ajax and all of its siblings like $.get or $.getJSON. I alway get an 200 OK but my Firebug reports an error. Printing the error just says: "error" - so not that helful.

I read that maybe the JSON file is corrupt. So I tried it with a plain text file (*.txt). Same result.

The JSON file is valid, I check it against a validator.

I also tried ContentType and dateType and experimented with json and jsonp...

I basically used something like this (with a million variations for testing purposes):

$.ajax({
   url: 'http://www.myurl.com/api/v1/myfile.json',
   ...
   success: function(data) {
      console.log(data);
    },
   error: function(error) {
     console.log(error.statusText);
   }
});

Am I missing something important here? It's really odd that nothing seems to change the behavior of the AJAX-call.

In fact I don't really need AJAX because I need to grab the JSON file when loading the page...

And the JSON file is not on the same domain as the AJAX caller.

7
  • no one will be able to help with just error, make a blank HTML page with just that request and see if it still occurs? Commented Sep 4, 2012 at 13:41
  • 2
    The JSON file itself may be malformed, can you provide a link to the actual JSON file you are trying to access? Commented Sep 4, 2012 at 13:42
  • try to use a JSON Parser to check if the response is really a valid JSON string Commented Sep 4, 2012 at 13:46
  • sorry, forgot to point out that i already checked the json file. i updated my post. Commented Sep 4, 2012 at 13:46
  • @epoch: Good idea! But still no luck. The error.statusText is success and error.message is undefined... Commented Sep 4, 2012 at 14:10

4 Answers 4

3

Is that URL located on the same server you're trying to get the data from?

If not, you ran into a cross-domain request, which can only be handled using JSONP. So, the JSON file itself must be compatible with JSONP format, otherwise jQuery won't be able to process it (even if you provide a 'jsonp' dataType).

P.S.: Firebug will always show response code 200 but give an empty response body for such requests

Sign up to request clarification or add additional context in comments.

3 Comments

how can i check if a json file is jsonp compartible?
check JSONP docs and this URL from Google Feed API - that should provide you with a solid start
P.S.: if the web on the other side would allow you to request data from your server, it would also work for you - for that, they'd need to be sending cross-domain headers for you to allow it
1

Try in this way by disabling security

$.ajax( {
        type : 'GET',
        contentType : "application/json; charset=utf-8",
        url : surl,  \\specify  your url
        async : false,
        dataType : 'json',
        headers : {
            Accept : "application/json",
            "Access-Control-Allow-Origin" : "*"
        },
        crossDomain : true,
        success : SucceedFunc,
        error : function(data, textStatus, errorThrown) {
            console.log("error" + ' ' + JSON.stringify(data) + ' ' + textStatus + '  ' + errorThrown);
        }
    });
function SucceedFunc(data) {
               alert("success");
                   }

    }

2 Comments

I changed dataType to jsonp, then I get the following: error {"readyState":4,"status":200,"statusText":"success"} parsererror Error: jQuery18008677731226247496_1346769531305 was not called
add &callback=? at the end of the url as myurl.com/api/v1/myfile.json&callback=?
0

Did you try to catch the error the correct way?

$.ajax({
    url: 'http://www.myurl.com/api/v1/myfile.json',
    success: function(data) {
         console.log(data);
    },
    error: function(error) {
         console.log(error.message);
    }
});

1 Comment

yes, i used console.log("error: "+error.statusText). error.message returns undefined
0

If you are using chrome go to cmd prompt and run the chrome by disabling the security. You can disable security using pathwhere_chrome_is_located\chrome.exe --disable-web-security and run the html page. I think this may help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.