Several methods for submitting forms using Jquery ajax

Source: Internet
Author: User
Tags json

In jquery The Ajax submission form has a post and get way, and we can use Ajax to serialize the form $ (' #表单ID ') directly using the Get method. Serialize ().

Details are as follows:

One, $get way to submit a form

The Get () method loads information through a remote HTTP GET request

Format

$ (selector). Get (Url,data,success (RESPONSE,STATUS,XHR), DataType)

Instance

Use AJAX get requests to change the text of a DIV element:

$.get (' login.php ', {
ID: ' Robin ',
Password: ' 123456 ',
Gate: ' Index '
}, function (data, status) {
Data is the returned object, status is the requested State
alert (data);
Now assume that the server script will return a text "Hello, robin!. ",
Then the browser will pop up the dialog box to display the text
alert (status);
The result is success, error, and so on, but here is the function to run when successful
});

Displays the test.php return value (HTML or XML, depending on the return value):

Ajax serialization Form

$. Form.serialize (Namevaluepair)

Virtual a form, and set the name and value of the form control.

Parameters
Namevaluepair

Required option. Set up a virtual form control. This parameter is in the form of: {name1=value, name2=value2, ...}
return value

The string after the virtual form is serialized, in the form of: username=%e5%95%8a%e8%94%a1&password=123456

$.post (' Your URL ', $ ("form"). Serialize (), function (data) {
Your code
}
});

$.get (' Your URL ', $ ("form"). Serialize (), function (data) {
Your code
}
});

$.getjson (' Your URL ', $ ("form"). Serialize (), function (data) {
Your code
}
});

Second, the $POST way to submit the form

$.post

Jquery.post (URL, [data], [callback], [Type]): Asynchronous request using POST method
Parameters:

URL (String): The URL address where the request is sent.

Data (MAP): (optional) The information to be sent to the server, expressed as a Key/value key-value pair.

Callback (function): (optional) callback function on successful loading (this method is invoked only if response's return state is success)

$.post ("data.php", $ ("#firstName. Val ()"), function (data) {

$ ("#postResponse"). HTML (data.name);

"JSON"//Set the type of get data, so the data format is JSON type

);

Ajax

$.ajax (Opiton)

$.ajax ({

URL: "Ajax/ajax_selectpictype.aspx",

Data:{full: "Fu"},

Type: "POST",

DataType: ' JSON ',

Success:callback,

Error:function (er) {

Backerr (ER);

});

IV, $.getjson (Url,[data],[callback])


 $.getjson ("data.php", $ ("#firstName. Val ()"), function (jsondata) {
 
$ ("#getJSONResponse") . html (jsondata.id);} Without setting, the data type directly obtained is JSON,
  so calls need to use the Jsondata.id method
 
);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.