Post Details

What is JSON, and how is it used in AJAX?

np

Tue , Apr 25 2023

np

Simple form of ajax request is mentioned below-

we need to pass datatype to understand server in which format we are sending the data here we are using json type.

in following ajax request parameter like data and datatype is optional we can ignore if we have nothing to post.

ajax request: 

$.ajax({

  dataType: 'json',

  url: url,

  data: data,

  success: success

});

Json example :

{

  "items": [

    {

      "key": "First",

      "value": "value"

    }

           ]

}

How do you handle errors in AJAX requests?

The error block is used for handling errors in ajax request:

$.ajax({

   type: "POST",

   url: "MyUrl",

   data: "val1=test",

   success: function(result){

        // Do stuff

   },

   error: function(request,status,errorThrown) {

        // There's been an error, do something with it!

        // Only use status and errorThrown.

        // Chances are request will not have anything in it.

   }

 });

Leave a Reply

Please log in to Comment On this post.