Home > Article > Web Front-end > javascript jQuery $.post $.ajax用法_jquery
jQuery.post( url, [data], [callback], [type] ): Use POST method to make asynchronous requests
Parameters:
url (String): Send request URL address.
data (Map): (Optional) The data to be sent to the server, expressed in the form of Key/value pairs.
callback (Function): (Optional) Callback function when loading is successful (this method is called only when the return status of Response is success).
type (String): (optional) The official description is: Type of data to be sent. In fact, it should be the type of client request (JSON, XML, etc.)
This is a simple POST request function to replace the complex $.ajax. The callback function can be called when the request is successful. If you need to execute a function on error, use $.ajax. Sample code:
Ajax.aspx:
Response.ContentType = "application/json";Response.Write("{result: '" Request["Name"] ",Hello! (This message comes from the server)'}");jQuery code:
$.post("Ajax.aspx", { Action: "post", Name: "lulu" }, function (data, textStatus){ // data can be xmlDoc, jsonObj, html, text, etc. //this; // . "); Click to submit:
Here the request format is set to "json":
$.ajax() This is the underlying AJAX implementation of jQuery. For simple and easy-to-use high-level implementations, see $.get, $.post, etc.
There are several Ajax event parameters here: beforeSend, success, complete, error. We can define these events to handle each of our Ajax requests well.
$.ajax({url: 'stat.php',
type: 'POST',
data:{Name: "keyun"},
dataType: 'html',
timeout: 1000,
error: function(){alert('Error loading PHP document');},
success : function(result){alert(result);}
});