Home >Web Front-end >JS Tutorial >AJAX uses post to send data in xml format and receive data
This article mainly introduces how AJAX uses post to send data in xml format and accepts data. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
AJAX uses post to send data xml. The format accepts data, friends who need it can refer to
Notes:
1. Use POST to send data, and the function on line 2 (also the function for ajax to send data: ajaxCall) must be added One sentence: xmlObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
Then use xmlObject.send(data); to send
2.3 line Note on the function:
1. Disable caching (recommended, not necessary): header("Cache-Control:no-cache");
2. When using XML data format, you must add: header("Content-Type: text/xml; charset=gb2312");//Write XML here
3. If you use MYSQL installed in the WAMP5 integrated environment, when querying the database, you must add:
$charset = "gb2312";
mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary"); //This sentence is necessary to solve the problem of Chinese garbled encryption Problem s
Otherwise, the encryption will be garbled. I wasted a long time here today. I used the database installed by default in the ECSHOP GBK version.
4. If you use XML to receive data, callback function It must be processed separately for IE and non-IE, otherwise there will always be one party that cannot marry the XML data
The processing code is as follows:
function getXMLData(tagName)//获取XML数据,分IE和非IE处理 { var info; if(window.ActiveXObject) //IE取回XML文件方法 { var doc = new ActiveXObject("MSxml2.DOMDocument"); doc.loadXML(xmlObject.responseText); info = doc.getElementsByTagName(tagName); } else //---------------------------非IE取回XML文件方法 { info = xmlObject.responseXML.getElementsByTagName(tagName); } return info; }
The following is a province and city linkage test I did
The code is as follows:
index.php省事联动测试
chuli.php <?php//3号线header("Cache-Control:no-cache"); header("Content-Type: text/xml; charset=gb2312");//这里要写XML require("function.php"); $id = $_POST['id']; file_put_contents("my1.txt",$act . "------" . $ziduan); $result = getresultById($id); $info = "<mes>"; foreach($result as $row){$info .= "<res>"; $info .= "<id>" . $row['region_id'] . "</id>"; $info .= "<name>" . $row['region_name'] . "</name>"; $info .= "</res>";} $info .= "</mes>"; echo $info; ?>
3. Database function
function.php <?php function getresultById($id) { $con = mysql_connect("localhost","root",""); if($con) { $charset = "gb2312"; mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary"); //这句是必须的,解决中文乱码加密问题s mysql_select_db("ajax",$con); $sql = "select * from ecs_region where parent_id = '$id'"; $res = mysql_query($sql); $arr = array(); while($row = mysql_fetch_assoc($res)) { $arr[] = $row; } return $arr; } return false; }
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
ajax realizes the effect of changing the input box text and displaying the drop-down list
Automatically through JS code in Ajax Get form element value
#
The above is the detailed content of AJAX uses post to send data in xml format and receive data. For more information, please follow other related articles on the PHP Chinese website!