当前位置: 移动技术网 > IT编程>开发语言>.net > AJAX使用post发送数据xml格式接受数据

AJAX使用post发送数据xml格式接受数据

2017年12月12日  | 移动技术网IT编程  | 我要评论

王乃恩渣男,兰炼二中贴吧,狂野目标插曲

注意点:

 1. 用post发送数据,在2号线函数(也是ajax发送数据的函数:ajaxcall)必须加上一句:xmlobject.setrequestheader("content-type","application/x-www-form-urlencoded");

接着使用xmlobject.send(data);发送

2.3号线函数要注意:

  1.禁用缓存(建议,不必要):header("cache-control:no-cache");

  2.使用xml数据格式必须加上:header("content-type: text/xml; charset=gb2312");//这里要写xml

  3.若使用wamp5集成环境安装的mysql,在查询数据库时候,必须加上:

    $charset = "gb2312";

    mysql_query("set character_set_connection=$charset, character_set_results=$charset, character_set_client=binary"); //这句是必须的,解决中文乱码加密问题s

   否则就会乱码加密,今天我就是在这里浪费了很久时间,我是用ecshop gbk版 默认安装的数据库

 4.若用xml接受数据,回调函数必须分ie和非ie处理,否则总是有一方娶不到xml数据

  处理代码如下:

  

复制代码 代码如下:

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;
}


 

下面就是我做的一个省市联动测试


代码如下:

index.php

复制代码 代码如下:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>省事联动测试</title>
<style type="text/css" >
select{
width:100px;
}
</style>
<script type="text/javascript" >

 

var thisid = ""; //当前操作的selecti的d

 

var xmlobject; //ajax 对象全局变量,

 

function getajaxobject()//ajax 1号线,返回一个ajax 对象引擎
{
var xmlobject ;

if(window.activexobject)
{

xmlobject = new activexobject("microsoft.xmlhttp");
}
else
{
xmlobject = new xmlhttprequest();
}

return xmlobject ;
}

 

function ajaxcall(id) //ajax 二号线 ,这里采用 post 传递参数
{
xmlobject = new getajaxobject();

if(xmlobject)
{
var url = "chuli.php";

var data = "id=" + id;

xmlobject.open("post",url,true);

 

xmlobject.setrequestheader("content-type","application/x-www-form-urlencoded");

xmlobject.onreadystatechange = repayfuncion;

xmlobject.send(data);

}

}


function repayfuncion() //ajax 四号线 ,这里采用 xml 接受数据,这里还涉及到xmldom编程
{


if(xmlobject.readystate==4 && xmlobject.status==200)
{


var info = getxmldata("res");//获取xml数据

$(thisid).length = 0;//清楚select 中的option节点

for(i=0;i<info.length;i++)
{

var optionid = info[i].childnodes[0].childnodes[0].nodevalue;

var optionvalue = info[i].childnodes[1].childnodes[0].nodevalue;

var optionnode = document.createelement('option');

optionnode.value = optionid;

optionnode.innertext =optionvalue;

$(thisid).appendchild(optionnode);

}

}

}


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;
}

function $(id)//常用函数,通过id取对象
{
return document.getelementbyid(id);
}

function getprovice()//获取省
{
thisid = "province";

var id = '1';

ajaxcall(id);

}

function getcity()//获取市
{
thisid = "city";

$("county").length = 0;

var id = $("province").value;

ajaxcall(id);

}

 

function getcounty()//获取县城
{
thisid = "county";

var id = $("city").value;

if($("city").length)
{
ajaxcall(id);
}

}

window.onlaod = getprovice();//页面开始载入省

</script>
</head>

<body>
<form action="javascript:void(0)" method="post">
<label for="username" >用户名:</label> <input type="text" name="username" id="username" width="60px" /><br />
<label for="psd" >密  码:</label> <input type="password" name="psd" id="psd" width="80px" /></br>
<label for="psd" >地  址:</label>
<select id="province" onclick="getcity()">
</select> 

<select id="city" onclick="getcounty()" >
</select> 

<select id="county" name="xian" >
</select>
<input type="submit" value="提交" />
</form>
</body>
</html>

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.数据库函数


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;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网