How to parse json response and add functions like error, complement it with jQuery?

I am working on creating a gadget using Json parsing. The output I get is by clicking the url http: // localhost: 3000 / cities.json below

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

I analyzed this with a function

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

Now I want to add the .error function to it, so if there is any problem with the answer or say the server is not responding, I can find out by adding a warning, for example

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

I tried it as follows

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

I also tried using this method

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

But none of them work. To check for an error, I stop my local server and it does not give me any warnings about it. Please tell me which direction should I continue?

------ 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 working, it gives me a warning and a second success, but in case I donโ€™t stop it, throw an error by also making the URL only http: // localhost: 3000 / cities.json always give an error regardless of server operation or not

+4
source share
5 answers

It 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

0
source

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 }); 
+1
source

The function you are using now is simply a shorthand for $. ajax looking like this:

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

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

0
source

If you are creating json data, you must create a parameter called error. By default, set to false and true if an error occurs. Then check the error value when you get the JSON.

Otherwise, I would recommend using

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

The last method should work, but you must specify the function for the 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 }); 
0
source

All Articles