getJSON does not work

Asked

Viewed 1,196 times

2

it does not return the success value, but in the error it says it was all right

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

Imagery: inserir a descrição da imagem aqui

3 answers

2

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

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

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

2


In accordance with the documentation the jQuery.ajax (and their "cousins") use Deferred (as in your code), in case you can’t see the error, because you’re catching the 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’re using a server like Apache (http://localhost) it is possible that the file is in another folder and you have to fix the note, for example if the en-us.json and the jquery.js are in the folder ./js but your html is in the root folder, jQuery will look in the root folder instead of the folder ./js, to correct point the correct folder in the $.geJSON (This is just one 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) is likely to be trying to access via Scheme URI file, note that Ajax does not work with the file://' protocol for browser security measures, so use a local HTTP server of your preference.

  • If you are using HTTP the path is right, then it may be the allowed folder permissions or extensions (as in the case of IIS servers), in this case you need to release the folder permission (on like-Unix systems for example) or configure the IIS (if you are the case) to allow showing 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 in this case should be Content-Type: application/json, for this it is necessary to configure your server.

    • Apache would be something like:

      Addtype application/json . json

    • IIS is something like:

      Open the IIS Manager View IIS Server properties Click on MIME Types and in Add Add the extension:

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

      Go back to IIS Server properties Click on Handler Mappings Add a script map

      • Request path: *.json
      • Executable: C: WINDOWS system32 inetsrv Asp.dll
      • Name: JSON

0

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.