Jquery: Send Json Objects With An Ajax Request

The jQuery post() method sends asynchronous http POST request to the hệ thống khổng lồ submit the data khổng lồ the hệ thống and get the response.

Bạn đang xem: Jquery: send json objects with an ajax request

Parameter Description:

url: request url from which you want to submit và retrieve the data. data: json data to lớn be sent lớn the hệ thống with request as a size data. callback: function to lớn be executed when request succeeds. type: data type of the response content.

Let"s see how to submit data & get the response using post() method. Consider the following example.

$.post("/jquery/submitData", // url myData: "This is my data." , // data to be submit function(data, status, jqXHR) // success callbachồng $("p").append("status: " + status + ", data: " + data); )p>p>
In the above sầu example, first parameter is a url to lớn which we want to lớn skết thúc http POST request and submit the data.

Internally, post() method calls ajax() method with method option to POST. Visit james.padolsey.com/jquery và search for post() method lớn see the jQuery source code.

The second parameter is a data to submit in JSON format, where key is the name of a parameter and value is the value of parameter.

The third parameter is a success callbaông chồng function that will be called when request succeeds. The callbaông chồng function can have three parameters; data, status và jqXHR. The data parameter is a response coming from the hệ thống.

The following example shows how to submit and retrieve JSON data using post() method.

Xem thêm: Trong 12 Cung Hoàng Đạo Bo Cap, Cung Bọ Cạp (23/10

$.post("/submitJSONData", // url myData: "This is my data." , // data khổng lồ be submit function(data, status, xhr) // success callbachồng function alert("status: " + status + ", data: " + data.responseData); , "json"); // response data format
In the above sầu example, please notice that last parameter is a type of response data. We will get JSON data as a hệ thống response. So post() method will automatically parse response into lớn JSON object. Rest of the parameters are same as first example.

You can also attach fail & done callbachồng methods khổng lồ post() method as shown below.

$.post("/jquery/submitData", myData: "This is my data." , function(data, status, xhr) $("p").append("status: " + status + ", data: " + data); ).done(function() alert("Request done!"); ) .fail(function(jqxhr, settings, ex) alert("failed, " + ex); );p>p>

Points khổng lồ Rethành viên :
$.post() method allows you khổng lồ skết thúc asynchronous http POST request to lớn submit và retrieve the data from the hệ thống without reloading whole page. Syntax: $.post(url,,,) Specify type parameter for the type of response data e.g. specify "JSON" if hệ thống return JSON data. Internally post() method calls ajax() method only by passing method="POST" as option.
obatambeienwasirherbal.com is optimized for learning web technologies step by step. Examples might be simplified lớn improve sầu reading & basic understanding. While using this site, you agree khổng lồ have sầu read & accepted our terms of use và privacy policy.

Subscribe khổng lồ obatambeienwasirherbal.com tin nhắn danh sách and get lakiểm tra updates, tips & tricks on C#, .Net, JavaScript, jQuery, AngularJS, Node.js to your inbox.