Notes for form submission in ajax post mode ., Ajaxpost

Source: Internet
Author: User

Notes for form submission in ajax post mode ., Ajaxpost

When we create an asynchronous object XMLHttpRequest and transmit data to the background in the post mode at the same time.

Set the value of the xhr. setRequestHeader Member of the asynchronous object

XMLHttpRequest. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded"); otherwise, the backend cannot receive the value passed in. Because Google Chrome's compiler shows that the pass value

In Request Payload, this is incorrect () -- check the attribute.



The correct method is in Form Data ()

This setting specifies how to encode the form data before it is sent to the server. There are three forms, which can be found in w3c documentation,

Example: <form enctype = "application/x-www-form-urlencoded"> </form>

There is a method serialize () in jquery .. The parameter can be set to a nest. Change to id = "value" & name = "value"

How does ajax post a form?

EncodeURIComponent for the url

The following is a program for submitting forms using post in AJAX.

Like your code, it is a good habit to end or end. the wrong code is like:
If (year. length = 0 ){
Document. getElementById ("txtHint"). innerHTML = "";
Return;
}
Where do you define year in ????????? Change to document. getElementById ("year "). value. At the same time, add an ID attribute to the INPUT. Otherwise, it is ByName. After this is changed, the program runs correctly. Please adopt it. I have passed the local test.

Or change to f. year. value Based on your ideas is also feasible.

Complete executable code:
Function showHint2 (){
Debugger;
Var req;
Var f = document. myform;
If (f. year. value. length = 0 ){
Document. getElementById ("txtHint"). innerHTML = "";
Return;
}
If (window. XMLHttpRequest)
Req = new XMLHttpRequest ();
Else
Req = new ActiveXObject ("Microsoft. XMLHTTP ");
Req. onreadystatechange = function (){
If (req. readyState = 4 ){
If (req. status = 200 ){
Alert ("2 ");
Var meg = req. reponseText;
Document. getElementById ("txtHint"). innerHTML = req. responseText;
}
}
}

Var username = f. username. value;
Var year = f. year. value;
Var post1 = "username =" + username + "& year =" + year;
Var url = "test. php ";
Req. open ("POST", url, true );
Req. setRequestHeader ("CONTENT-TYPE", "applic ...... the remaining full text >>>

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.