$. Ajax method in jquery: submitting a form _ jquery-js tutorial

Source: Internet
Author: User
This article mainly introduces the $. ajax method in jquery to submit forms. I hope it will be helpful to you. The Code is as follows:


Function postdata () {// submit data function
$. Ajax ({// call jquery's ajax Method
Type: "POST", // set the ajax method to submit data
Url: "OK. php", // submit the data to OK. php
Data: "writer =" + $ ("# writer"). val (), // the value in the writer field in the input box is used as the submitted data.
Success: function (msg) {// callback after successful submission. The msg variable is output by OK. php.
Alert ("data submitted successfully"); // if necessary, you can display the msg variable value to a DIV element.
}
});
}

Jquery manual description:
The data sent to the server. Will be automatically converted to the request string format. The GET request will be appended to the URL. View the description of the processData option to disable automatic conversion. It must be in Key/Value format. If it is an array, jQuery automatically corresponds to the same name for different values. For example, {foo: ["bar1", "bar2"]} is converted to '& foo = bar1 & foo = bar2 '.
Example:

The Code is as follows:


$. Ajax ({
Type: "POST ",
Url: "some. php ",
Data: "name = John & location = Boston ",
Success: function (msg ){
Alert ("Data Saved:" + msg );
}
});

Here, the parameters following data can be written in two forms: one is to write common url parameters, and the other is to write them in a json array,
The data section in the preceding example can also be written as follows: data: {name: "John", location: "Boston "}. What are the differences between the two statements?

Today, I found a slight difference in the usage of the two in development. First, we use the url to pass the parameter. If the parameter contains the "&" symbol, the parameter may not be received or is incomplete, for example, "data: "name = John & location = Boston ",",

If the name value is "john & smith", this may cause a problem. We can escape it using the encodeURIComponent () method in JS,

However, if you use data: {name: "John", location: "Boston"}, you do not need to escape it. If you escape it, you will receive the escaped string.

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.