4

I am working on making a mini app using Json parsing . Output I am getting by hitting url http://localhost:3000/cities.json is as below

[
    {
    "id": 1,
    "name": "Bangalore"
    },
    {
    "id": 2,
    "name": "Chandigarh"
    },
    {
    "id": 3,
    "name": "Chennai"
    },
    {
    "id": 4,
    "name": "Hyderabad"
    },
]

I have parsed this using function

  $.getJSON("http://localhost:3000/cities.json?&callback=?", function(data) {
                //something something
 }

Now I want to add .error function to it so in case there is some issue with response or say server doesnt respond i may get to know about it say by putting an alert like

.error(function(){alert("error");})

I tried it in following way

  $.getJSON("http://localhost:3000/cities.json?&callback=?", function(data) {
                //something something
         }).error(function(){
            alert("error");
         })

I tried it using this way as well

     var cities = $.getJSON("http://localhost:3000/cities.json");
                   cities.error("hi");        

But none of them is working. To check for error i stop my local server and it doesnt give me any alert for that . Please guide me which way should i proceed ?

------EDIT--------

Also tried using

 var jqxhr = $.getJSON("http://localhost:3000/cities.json?callback=?", function() {
          alert("success");
        })
        .success(function() { alert("second success"); })
        .error(function() { alert("error"); })
        .complete(function() { alert("complete"); });

in case my localhost:3000 server is running it give me alert success and second success but in case i stop it no error call, also making url just http://localhost:3000/cities.json always tend to give error irrespective of server running or not

5 Answers 5

1

you can use ajaxSetup

$.ajaxSetup({
  error:function(XMLHttpRequest) {   
    //error
    console.log(XMLHttpRequest.responseText);
}});

and your getJSON goes as usual

  $.getJSON("http://localhost:3000/cities.json?&callback=?", function(data) {
                //something something
 });
Sign up to request clarification or add additional context in comments.

8 Comments

is it this way ?? $.getJSON("localhost:3000/cities.json?&callback=?", function(data) { alert("hi"); } $.ajaxSetup({ "error":function(XMLHttpRequest) { alert("hello"); }});
first define the ajaxSetup like in the answer and then define your $.getJSON the order matters
are there any errors in the firebug console? have you included the jquery? have you wrapped the code inside $(document).ready(function(){//your code});? are you specifying the correct path?
jsfiddle.net/BRTB3 ...and ...jsfiddle.net/WNF7W here are the code i wrote and it shows no error in 'firebug (firefox)'...... when server is running it alerts hi and when its stopped no hello. Code is in '$(document).ready(function(){})' ... also changing cities to citiies or something else aso gives no error
have you checked the path to the file, everything seems to be fine, only thing left is cross-domain issue, can you see the ajax call made in the firebug>Net>XHR tab?
|
0

Function that you use now is just a shorthand for for $.ajax looking like this:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

If you change your code to $.ajax, you can use error the same way you use success. Or alternatively you can use $.ajaxSetup to set default error handler for your ajax calls.

1 Comment

1. url: "localhost:3000/cities" 2. url: "localhost:3000/cities.json" 3. url: "localhost:3000/cities.json?callback=?" none of them gives success: function(){alert("hello");}
0

If you are creating the json data, you should create a parameter called error. Set it to false by default, and true if an error arises. Then check the error value when you getJSON.

Otherwise, I might recommend using

.get("", {}, function(json){
   try{
     var obj = jQuery.parseJSON(json);
   }catch(err){ console.log(err); }
});

Comments

0

You last method should work but you have to specify a function for error:-

var cities = $.getJSON("http://localhost:3000/cities.json");
cities.error(function(){
    alert('error');
});

OR you can use:-

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback,
  error: callback
});

Comments

0

This worked for me finally

           $.ajax({
            url: "http://localhost:3000/cities.json?&callback=?",
            type: "GET",
            dataType: "json",
            timeout: 10000,
            success: function(data, status, xmlstatus) {
                call_to_function(data);
            },
            error: function(x, t, m) {
                if (t === "timeout") {
                    $('#spinner').hide();
                } else {
                    $('#spinner').hide();
                }
            }
        });

Thanks

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.