Skip to main content

AJAX and JSON

JSON – Javascript Serialized Object Notation

JSON has key-value pair

Following is the format of JSON
{name: "Nilesh", age: 31, city: "Kolhapur"}

We will submit data using ajax and get a response as JSON
$.ajax({ 
    type:'POST',   
    url:'submit.php', 
    data:{ name: "Abc" }, 
    dataType: 'json', 
    success:function(data){
        console.log( data.name ); 
    }, 
    error:function(data) { 
        alert(data); 
    } 
}); 

POST method
$.post( "submit.php", { name: "Abc" },  function( data ) { 
     console.log( data.name ); 
}, "json"); 

Following is shortform
$.getJSON( "submit.php", function( data ) {
     console.log( data.name ); 
});

Comments