Ajax Post Way Send request server cannot receive data problem solution

Source: Internet
Author: User
Tags php and

See the source directly

The code is as follows Copy Code

<script language= "JavaScript" >
function Setviewpoint () {
var msg = ""; Used to receive returned data
Instantiating Ajax
var leeajax = Leeinitajax ();
var webRoot = window.location;
WebRoot = Webroot.tostring ();
var domain = webroot.substring (7);
var endd = Domain.indexof ("/");
Domain = domain.substring (0,endd);
Receive the URL address for the form
var url = "http://" +domain+ "/plus/leegetviewforway.php";
To open a connection by post
Leeajax.open ("POST", url, True);
Defines the transmitted file HTTP header information
Leeajax.setrequestheader ("Content-type", "APPLICATION/X-WWW-FORM-URLENCODED;CHARSET=GBK");
The value of the post is required to join each variable by &
Send post data
var poststr = "viewid= ' asdf ' &id= ' Asdfa '"
Leeajax.send (POSTSTR),//Get execution status
Leeajax.onreadystatechange = function () {
If the execution status succeeds, the return information is written to the specified layer
if (leeajax.readystate = = 4 && leeajax.status = 200) {
msg = leeajax.responsetext;//obtained return value
Alert (msg);
}
}
}
function Leeinitajax () {
var Leeajax=false;
try{
if (window. ActiveXObject) {
for (var i = 5; i; i--) {
try{
if (i = = 2) {
Leeajax = new ActiveXObject ("Microsoft.XMLHTTP");
} else {
Leeajax = new ActiveXObject ("msxml2.xmlhttp." + i + ". 0");
}
Break
catch (e) {
Leeajax = false;
}
}
else if (window. XMLHttpRequest) {
Leeajax = new XMLHttpRequest ();
if (Leeajax.overridemimetype) {
Leeajax.overridemimetype (' Text/xml ');
}
}
catch (e) {
Leeajax = false;
}
return leeajax;
}
</script>

The PHP code is as follows:

The code is as follows Copy Code

<?php
$a = $_request ["Viewid"]. ") ___ (". $_request[" id "];
if ($_post ["Viewid"]== ") $a. =" <br/>___ ";
echo $a;
?>


So in the building to send data from the string into a JS object, but still not. Had to check the data. The original is to set the encoding type of data sent. The code is as follows:

The code is as follows Copy Code
var myquery = Myinput ();

Xmlhttp.open ("POST", "post.php", true);

Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");

Xmlhttp.onreadystatechange = byphp;


Xmlhttp.send (myquery); The application/x-www-form-urlencoded encoding type is the default encoding type for the form to send data, and is not expected to be explicitly stated in an AJAX request, This adds a third line of code to indicate the format is correct, the problem solved. It is much easier to send a post-mode Ajax request via jquery without adding this line. Once again I feel the power and convenience of jquery.


To send an AJAX asynchronous request with post, you will be sending your data uniformly in the Send () method, not send (null).

The code is as follows Copy Code
Xml.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ');

This sentence is not written in get mode, but it is necessary in the post mode, because you use Send (NULL) in the post mode, which causes no data to be sent to the index1.php, so you cannot see the effect of this sentence.

The code is as follows Copy Code

Get (' index1.php?c=1 ', a) "

Get (' index1.php?c=2 ', a) "

Two buttons send the data only c This parameter is different, so you can put index1.php and a are written in the inside of your get method, the modified get method is like this

The code is as follows Copy Code
function get (num)
{
Data= "index1.php";
if (XML) {
var Obj=document.getelementbyid ("a");
Xml.open ("POST", data);
Xml.onreadystatechange=function ()
{
if (xml.readystate==4&&xml.status==200) {
Obj.innerhtml=xml.responsetext;
}
}
Xml.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
var querystring= "c=" +num;
Xml.send (querystring);
}
}

Of course

The code is as follows Copy Code

Get (' index1.php?c=1 ', a) "

Get (' index1.php?c=2 ', a) "

We should change it accordingly.

Get (1);

Get (2);

This enables you to successfully request data asynchronously from the server using the Post 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.