Home  >  Article  >  Web Front-end  >  javascript jQuery $.post $.ajax usage

javascript jQuery $.post $.ajax usage

巴扎黑
巴扎黑Original
2017-07-03 10:32:541354browse

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.

jQuery.post( url, [data], [callback], [type] ): Use POST method to make asynchronous requests


Parameters:

url (String): The URL address to send the request.

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"] + ",you OK! (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; // For the option configuration information of this Ajax request, please refer to this mentioned in jQuery.get() alert(data .result);     }, "json"); Click to submit:

The request format is set to "json" here:


$.ajax() This is the bottom layer of jQuery AJAX implementation. For simple and easy-to-use high-level implementations, see $.get, $.post, etc.

There are several Ajaxeventparameters: 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);}

});

The above is the detailed content of javascript jQuery $.post $.ajax usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn