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:
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:
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);
});
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:
.json
application/json
Go back to the IIS Server properties
Click% with%
Add a Handler Mappings
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 .
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.