Saturday, 24 August 2013

Ajax call Jquery


The ajax() method is used to perform an AJAX (asynchronous HTTP) request.

All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.



Definition :

  $.ajax({name:value, ... })



Usage:

/* get some values from elements on the page: */

var username= $('#username').val();
var pass=$('#pass').val();
//parameters to pass
 var data = {
    username : username,
    pass:pass    
   };
//authenticateUser is the action which you need to call
var testUrl = "user/authenticateUser";
$.ajax({
 url : testUrl,
 type : 'post',
 data : data,
 dataType : 'json',
 success : function(data) {
    //success action Block
    alert("Login success");
  },
   error:function(){
    //Error action block
    alert("Login failed");
   }
});


or you can achieve via this way.


var id = '1'
var testUrl = "user/authenticateUser";
var request = $.ajax({
  url: testUrl,
  type: "POST",
  data: {id : id},
  dataType: "html"
});

request.done(function(msg) {
  $("#log").html( msg );
});

request.fail(function(jqXHR, textStatus) {
  alert( "Request failed: " + textStatus );
});

No comments:

Post a Comment

Spring boot with CORS

CORS (Cross-Origin Resource Sharing) errors occur when a web application running in a browser requests a resource from a different domain or...