GET method {code ...} result: string (4) & quot; POST & quot; Notice: Undefinedindex: name1inD: phpstudyWWWservice. phponline23Notice: Undefinedindex: numberinD: phpstudyWWWservice. phponline24Notice: Undefi...
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:
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.