佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > AJAX使用post發(fā)送數(shù)據(jù)xml格式接受數(shù)據(jù)

AJAX使用post發(fā)送數(shù)據(jù)xml格式接受數(shù)據(jù)

熱門(mén)標(biāo)簽:Mysql連接數(shù)設(shè)置 Linux服務(wù)器 電子圍欄 服務(wù)器配置 銀行業(yè)務(wù) 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 團(tuán)購(gòu)網(wǎng)站 阿里云

注意點(diǎn):

 1. 用POST發(fā)送數(shù)據(jù),在2號(hào)線函數(shù)(也是ajax發(fā)送數(shù)據(jù)的函數(shù):ajaxCall)必須加上一句:xmlObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

接著使用xmlObject.send(data);發(fā)送

2.3號(hào)線函數(shù)要注意:

  1.禁用緩存(建議,不必要):header("Cache-Control:no-cache");

  2.使用XML數(shù)據(jù)格式必須加上:header("Content-Type: text/xml; charset=gb2312");//這里要寫(xiě)XML

  3.若使用WAMP5集成環(huán)境安裝的MYSQL,在查詢(xún)數(shù)據(jù)庫(kù)時(shí)候,必須加上:

    $charset = "gb2312";

    mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary"); //這句是必須的,解決中文亂碼加密問(wèn)題s

   否則就會(huì)亂碼加密,今天我就是在這里浪費(fèi)了很久時(shí)間,我是用ECSHOP GBK版 默認(rèn)安裝的數(shù)據(jù)庫(kù)

 4.若用XML接受數(shù)據(jù),回調(diào)函數(shù)必須分IE和非IE處理,否則總是有一方娶不到XML數(shù)據(jù)

  處理代碼如下:

  

復(fù)制代碼 代碼如下:

function getXMLData(tagName)//獲取XML數(shù)據(jù),分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;
}


 

下面就是我做的一個(gè)省市聯(lián)動(dòng)測(cè)試


代碼如下:

index.php

復(fù)制代碼 代碼如下:

!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>省事聯(lián)動(dòng)測(cè)試/title>
style type="text/css" >
select{
width:100px;
}
/style>
script type="text/javascript" >

 

var thisId = ""; //當(dāng)前操作的selectI的D

 

var xmlObject; //ajax 對(duì)象全局變量,

 

function getAjaxObject()//AJAX 1號(hào)線,返回一個(gè)AJAX 對(duì)象引擎
{
var xmlObject ;

if(window.ActiveXObject)
{

xmlObject = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
xmlObject = new XMLHttpRequest();
}

return xmlObject ;
}

 

function ajaxCall(id) //ajax 二號(hào)線 ,這里采用 post 傳遞參數(shù)
{
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 四號(hào)線 ,這里采用 xml 接受數(shù)據(jù),這里還涉及到xmldom編程
{


if(xmlObject.readyState==4 xmlObject.status==200)
{


var info = getXMLData("res");//獲取XML數(shù)據(jù)

$(thisId).length = 0;//清楚select 中的option節(jié)點(diǎn)

for(i=0;iinfo.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數(shù)據(jù),分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)//常用函數(shù),通過(guò)ID取對(duì)象
{
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();//頁(yè)面開(kāi)始載入省

/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" >密 nbsp;碼:/label> input type="password" name="psd" id="psd" width="80px" />/br>
label for="psd" >地 nbsp;址:/label>
select id="Province" onclick="getCity()">
/select>nbsp;

select id="City" onclick="getCounty()" >
/select>nbsp;

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

chuli.php

復(fù)制代碼 代碼如下:

?php
//3號(hào)線
header("Cache-Control:no-cache");

header("Content-Type: text/xml; charset=gb2312");//這里要寫(xiě)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.數(shù)據(jù)庫(kù)函數(shù)


function.php

復(fù)制代碼 代碼如下:

?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"); //這句是必須的,解決中文亂碼加密問(wèn)題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;
}

您可能感興趣的文章:
  • 用Ajax讀取xml文件的簡(jiǎn)單例子
  • jQuery+ajax讀取并解析XML文件的方法
  • Jquery Ajax學(xué)習(xí)實(shí)例 向頁(yè)面發(fā)出請(qǐng)求,返回XML格式數(shù)據(jù)
  • javascript解析ajax返回的xml和json格式數(shù)據(jù)實(shí)例詳解
  • jquery $.ajax()取xml數(shù)據(jù)的小問(wèn)題解決方法
  • JS通過(guò)ajax動(dòng)態(tài)讀取xml文件內(nèi)容的方法
  • 用Ajax讀取XML格式的數(shù)據(jù)
  • 通過(guò)AJAX的JS、JQuery兩種方式解析XML示例介紹
  • JS使用ajax從xml文件動(dòng)態(tài)獲取數(shù)據(jù)顯示的方法
  • 用JQuery 實(shí)現(xiàn)AJAX加載XML并解析的腳本
  • Ajax對(duì)xml信息的接收和處理操作實(shí)例分析

標(biāo)簽:江蘇 蚌埠 棗莊 衢州 萍鄉(xiāng) 衡水 廣元 大理

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《AJAX使用post發(fā)送數(shù)據(jù)xml格式接受數(shù)據(jù)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266
    鄂伦春自治旗| 中山市| 澎湖县| 尚志市| 监利县| 南康市| 承德市| 丽江市| 招远市| 聂拉木县| 浦江县| 噶尔县| 林口县| 大厂| 河津市| 山东省| 额敏县| 天长市| 沅江市| 四川省| 松原市| 五河县| 响水县| 周口市| 巴东县| 新干县| 高唐县| 定远县| 集贤县| 青海省| 灵川县| 枝江市| 绥芬河市| 右玉县| 灌阳县| 乌恰县| 红原县| 山阳县| 尚义县| 桓台县| 恩平市|