getJSON does not work

2

It does not return the value of success, but in error it says that everything is okay

$.getJSON( "en-us.json", function( json ) {
  console.log( "SUCESS");
 }).fail(function(m) {
    console.log(m);
});

Image:

    
asked by anonymous 09.04.2015 / 21:08

3 answers

2

According to documentation jQuery.ajax (and its "cousins") use Deferred (as in your code), in case you can not see the error, because you are getting jqXHR instead of textStatus or errorThrown :

jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});

To use in your code do this:

$.getJSON( "en-us.json", function( json ) {
  console.log( "SUCESS");
 }).fail(function(jqXHR, textStatus, errorThrown) {
    console.log(textStatus, errorThrown);
});

Possible causes of the problem

  • If you are using a server like Apache ( http://localhost ) it is possible that the file is in another folder and you have to correct the note, for example, if en-us.json and jquery.js are in ./js folder % but your html is in the root folder, jQuery will look in the root folder instead of the ./js folder, to fix the correct folder in $.geJSON (this is just an example):

    $.getJSON("js/en-us.json", function( json ) {
        console.log( "SUCESS");
    }).fail(function(jqXHR, textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
    });
    
  • If you are not using a pache server ( http://localhost ) you are probably trying to access via file URI scheme , note that Ajax does not work with the 'file: ///' protocol for browser security measures, so use a local HTTP server of your choice.

  • If you are using HTTP the path is right, then it may be the permissions of the folder or extensions allowed (as in the case of IIS servers), in this case it is necessary to release the permission of the folder (on like-unix systems for example) or configure IIS (if it is your case) to allow you to display files with the extension .json

  • If none of the above problems is yours, then we can assume that your server is not sending the correct header, which should be Content-Type: application/json , for this you need to configure your server.

    • In Apache it would be something like:

      AddType application / json .json

    • In IIS it looks something like:

      Open the IIS Manager View the IIS Server Properties Click MIME Types and Add Add extension:

      • File name extension: .json
      • MIME type: application/json

      Go back to the IIS Server properties Click% with% Add a Handler Mappings

      • Request path: * .json
      • Executable: C: \ WINDOWS \ system32 \ inetsrv \ asp.dll
      • Name: JSON
09.04.2015 / 21:46
2

The first parameter of $.getJSON() , according to your own documentation , should be a URL. This is because the method will access your resource via an HTTP request using the GET verb.

Apparently, en-us.json is not being found. Are you trying to access a file or a URL? If it is a file, the File API will help you; otherwise, specify a valid URL.

To give more ownership to my answer, I have prepared a jsFiddle with an operating example: check it here .

    
09.04.2015 / 21:26
0

So $.getJSON works normally what can be happening and that the function is not found the file or it is in an invalid format. Do a search on "json file format" you will find how to format a json file right, that's probably it.

    
09.04.2015 / 21:30