Ajax-return value-When ajax is used, why does get the value on the php page in the background, but post does not work?

Source: Internet
Author: User
GET method {code ...} result: string (4) & quot; POST & quot; Notice: Undefinedindex: name1inD: phpstudyWWWservice. phponline23Notice: Undefinedindex: numberinD: phpstudyWWWservice. phponline24Notice: Undefi...
  1. GET Method

Document. getElementById ("save"). onclick = function () {var request = new XMLHttpRequest (); request. open ("GET "," http://localhost/service.php ? Username = "+ document. getElementById ("stuname "). value + "& number =" + document. getElementById ("stunumber "). value + "& sex =" + document. getElementById ("stusex "). value + "& job =" + document. getElementById ("stujob "). value); request. send (null); request. onreadystatechange = function () {if (request. readyState = 4) {if (request. status = 200) {document. getElementById ("saveResult "). innerHTML = request. responseText;} else {alert (" Error la: "+ request. status) ;}}} service. php $ username = $ _ GET ['username']; $ password = $ _ GET ["number"]; $ sex = $ _ GET ["sex"]; $ job = $ _ GET ["job"]; if ($ username = "11") & ($ password) & ($ sex) & ($ job) {echo '{"success": "true", "msg": "student added successfully "}';} else {echo '{"success": "false", "msg": "parameter error, incomplete information"}';} result: Correct. However, there is a problem when it is passed in the form of post. The Code is as follows: document. getElementById ("save "). onclick = function () {var request = new XMLHttpRequest (); var data = "name1 =" + document. getElementById ("stuname "). value + "& number =" + document. getElementById ("stunumber "). value + "& sex =" + document. getElementById ("stusex "). value + "& job =" + document. getElementById ("stujob "). value; request. open ("POST "," http://localhost/service.php "); Request. setRequestHeader ("Content-Type", "appliaction/x-www-form-urlencoded"); request. send (data); request. onreadystatechange = function () {if (request. readyState = 4) {if (request. status = 200) {document. getElementById ("saveResult "). innerHTML = request. responseText;} else {alert ("the error la:" + request. status) ;}}} service. php code: var_dump ($ _ SERVER ['request _ method']); // for testing, use $ username = $ _ POST ['name1']; $ password = $ _ POST ["number"]; $ sex = $ _ POST ["sex"]; $ job = $ _ POST ["job"]; if ($ username = "11") & ($ password) & ($ sex) & ($ job) {echo '{"success ": "true", "msg": "student added successfully"} ';} else {echo' {"success": "false", "msg": "parameter error, incomplete Information "}';}

Result:

String (4) "POST"
Notice: Undefined index: name1 in D: phpstudyWWWservice. php on line 23
Notice: Undefined index: number in D: phpstudyWWWservice. php on line 24
Notice: Undefined index: sex in D: phpstudyWWWservice. php on line 25
Notice: Undefined index: job in D: phpstudyWWWservice. php on line 26
{"Success": "false", "msg": "parameter error, incomplete information entered "}
Why? Where do I have a problem? Please kindly advise. Thank you!

Reply content:
  1. GET Method

Document. getElementById ("save"). onclick = function () {var request = new XMLHttpRequest (); request. open ("GET "," http://localhost/service.php ? Username = "+ document. getElementById ("stuname "). value + "& number =" + document. getElementById ("stunumber "). value + "& sex =" + document. getElementById ("stusex "). value + "& job =" + document. getElementById ("stujob "). value); request. send (null); request. onreadystatechange = function () {if (request. readyState = 4) {if (request. status = 200) {document. getElementById ("saveResult "). innerHTML = request. responseText;} else {alert (" Error la: "+ request. status) ;}}} service. php $ username = $ _ GET ['username']; $ password = $ _ GET ["number"]; $ sex = $ _ GET ["sex"]; $ job = $ _ GET ["job"]; if ($ username = "11") & ($ password) & ($ sex) & ($ job) {echo '{"success": "true", "msg": "student added successfully "}';} else {echo '{"success": "false", "msg": "parameter error, incomplete information"}';} result: Correct. However, there is a problem when it is passed in the form of post. The Code is as follows: document. getElementById ("save "). onclick = function () {var request = new XMLHttpRequest (); var data = "name1 =" + document. getElementById ("stuname "). value + "& number =" + document. getElementById ("stunumber "). value + "& sex =" + document. getElementById ("stusex "). value + "& job =" + document. getElementById ("stujob "). value; request. open ("POST "," http://localhost/service.php "); Request. setRequestHeader ("Content-Type", "appliaction/x-www-form-urlencoded"); request. send (data); request. onreadystatechange = function () {if (request. readyState = 4) {if (request. status = 200) {document. getElementById ("saveResult "). innerHTML = request. responseText;} else {alert ("the error la:" + request. status) ;}}} service. php code: var_dump ($ _ SERVER ['request _ method']); // for testing, use $ username = $ _ POST ['name1']; $ password = $ _ POST ["number"]; $ sex = $ _ POST ["sex"]; $ job = $ _ POST ["job"]; if ($ username = "11") & ($ password) & ($ sex) & ($ job) {echo '{"success ": "true", "msg": "student added successfully"} ';} else {echo' {"success": "false", "msg": "parameter error, incomplete Information "}';}

Result:

String (4) "POST"
Notice: Undefined index: name1 in D: phpstudyWWWservice. php on line 23
Notice: Undefined index: number in D: phpstudyWWWservice. php on line 24
Notice: Undefined index: sex in D: phpstudyWWWservice. php on line 25
Notice: Undefined index: job in D: phpstudyWWWservice. php on line 26
{"Success": "false", "msg": "parameter error, incomplete information entered "}
Why? Where do I have a problem? Please kindly advise. Thank you!

Use $ _ post for POST
If you want get and post, you can use $ _ REQUEST.

Because you use $ _ GET in PHP, you can only get the value passed by GET.

$ _ GET url Connection Parameters
$ _ POST get parameters submitted by post
$ _ REQUEST can obtain get and post parameters.

In your case, $ _ GET cannot obtain the Post parameter.

The format of the parameter in the post request is incorrect. Replace it with var postData = {"name1": "value1", "name2": "value2. You can perform F12 debugging.

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.