Reputation: 8379
I have written a ajax request using getJSON of jQuery like below
$.getJSON('bDays.json', function (bDy) { // This file exists in same folder path
alert(1); // It is not executed..
});
Even if I change the code to get .txt file.. It is not working.. Kindly let me know what could be the issue with my code
Upvotes: 2
Views: 366
Reputation: 18101
Here are a couple things to try:
As pointed out by others, bDays.json
must be accessible via an HTTP request. Try replacing 'bDays.json'
in your getJSON
call with its full URL.
Make sure bDays.json
contains valid JSON (run it through a validator, such as this one, to be certain if it is not obvious). The file needs to be valid JSON, as stated in the documentation:
As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently.
Upvotes: 1
Reputation: 47280
getJSON uses a get HTTP request to retrieve json encoded data, it won't open a text file from your local drive.
The JSON data should be hosted on a webserver, and you can then reference its full URL.
Upvotes: 2
Reputation: 46008
Callback function is a third parameter:
$.getJSON('bDays.json', null, function (bDy) {
alert(1);
});
Also, try using a full url instead of just filename.
Upvotes: 1
Reputation: 87073
Check that you write the code within document.ready()
handler .
$(function() {
$.getJSON('bDays.json', function (bDy) {
alert(1);
});
});
One thing, $.getJSON()
can't parse or open a text file.
Upvotes: 1