[讨论]php 无法接收 ajax提交的 json 值 本帖最后由 phnessu4 于 2009-06-13 11:29:52 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好像网上有jquery,ajax,json的代码例子 http://www.phpweblog.net/fuyongjie/archive/2009/01/20/6290.html这里有个例子 //其他错误,显示状态码 xmlHttp.status 是什么?=====================ajax.php代码 服务端的代码处理何在?你要有返回数据呀?PHP code <!--引用json --> <script type="text/javascript" src="js/json.js"></script> <?php print_r($_POST); ?>改成<!--引用json --> <script type="text/javascript" src="js/json.js"></script> <?php echo "test"; ?>看能不能接受到值呢? $_POST数组中当然是有值的呀,但是你把这些值经过处理后,需要把值返回回去给客户端,客户端才能接受的到呀不知道,这些是否可以帮到你? 多谢....目前处于初学ajax阶段,想自己写点基本的东西,打算弄明白原理后再用jquery..... 以下为js代码(没找到js高亮标签)//回调方法function updatePage(){ //状态判断 if(xmlHttp.readyState == 4){ if (xmlHttp.status == 200){ var response = xmlHttp.responseText; alert(response); //404错误 }else if (xmlHttp.status == 404){ alert("Request URL does not exist"); //其他错误,显示状态码 }else{ alert("Error: status code is " + xmlHttp.status); } }}这里验证了,readyState必须要是4才能 alert关于firebug的调试,我看过header的信息,post里面是有值的,也传递过去了,但是php没有接收到,如果使用以下格式的代码,php可以接收到,并且返回正常,所以会很郁闷//获取子分类function getSubCat(cat) { //创建对象 createRequest(); var cid = "cid=1"; //设置服务器 URL var url = "ajax.php"; //建立到服务器的新请求 xmlHttp.open("POST", url, true); //告知服务器以text/xml方式接收 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); //指定回调方法 xmlHttp.onreadystatechange = updatePage; //向服务器发送请求 xmlHttp.send(cid);} 如果不是cid=1的这种格式的话,可以用file_get_contents("php://input");来获取. js稍微改改就是了:xmlHttp.send("parms="+ cid.toJSONString());然后php解析传过来的parms ajax============== //告知服务器以text/xml方式接收 xmlHttp.setRequestHeader('Content-Type', 'text/xml');//soap方式。php==============print_r( $GLOBALS['HTTP_RAW_POST_DATA']); //察看原始post数据 var response = xmlHttp.responseText; alert(response);接受xml好像不是用responseText吧?再说。您为什么一定要用post提交呢?get传值不是很好吗?再来个md5()什么东东都OK拉 多谢两位...两种方法都试过了,都是可行的,而且可以用来接收xml我还顺便vardump了一下,file_get_contents和$GLOBALS['HTTP_RAW_POST_DATA']的数据,都是以string的格式过来的.. 刚才看手册的时候有这么一段,看样file_get_contents("php://input")效率会好一些php://input 允许读取 POST 的原始数据。和 $HTTP_RAW_POST_DATA 比起来,它给内存带来的压力较小,并且不需要任何特殊的 php.ini 设置。php://input 不能用于 enctype="multipart/form-data"。 关于你说的get传值,如果数据量小的话还好,但是如果是一个json格式的数组或者是一页的xml,这时url的长度就会出现问题,所以还是post比较好一些.. //设置服务器 URL(get方式传值) var url = "ajax.php?cid="+escape(cid)+"&bid="+escape(bid)+.......;关于xml的接收...正在折腾.不过用var response = xmlHttp.responseXML;alert(response);收不到格式化的值...用xmlHttp.responseTEXT可以(这种格式只能是string)... 传值主要有GET和POST首先,另外,传值基本上是字串和流!如果你post一个a="json字串",服务器端是可以收的echo $_POST['a'],解析即可!另外,var response = xmlHttp.responseXML;alert(response); 这个是不行的xmlHttp.responseXML 返回的是xmlDOM对象,如果文件不xml文件,出错另外,xmlHttp.responseXML的默认属性不是字符串,所以alert(response); 出错多查查xmlhttp资料 多谢指点~~~刚才google了....数据当作xml返回时,php脚本少了一个header("Content-Type: text/xml; charset=utf8"); 怪不得接不到值,原来一直被当作string处理了...alert之后,提示的是 XML对象附带一小段xml解析的方式,friebug调试通过了,我用for循环做的,不知道大家是怎么取值的???有没有更简单的方法?xml代码<paper> <title>perl</title> <title>php</title> <title>js</title></paper>js代码如下function updatePage(){ //状态判断 if(xmlHttp.readyState == 4){ if (xmlHttp.status == 200 ){ /***************************** 接收数据 responseText (string格式) responseXML (xml object) *****************************/ //alert(xmlHttp.responseText); var xmlDoc = xmlHttp.responseXML.documentElement; alert(xmlDoc.nodeName); for (var x=0; x<xmlDoc.childNodes.length; x++) { //获取 title 元素 var titleElement = xmlDoc.getElementsByTagName('title')[x]; //alert(titleElement); //debug if(titleElement != null){ //获取第一个子元素 var titleText = titleElement.firstChild; //输出值 alert(titleText.nodeValue); } } //404错误 }else if (xmlHttp.status == 404){ alert("Request URL does not exist"); //其他错误,显示状态码 }else{ alert("Error: status code is " + xmlHttp.status); } }}附带一个js解析xml的DOM文档http://dev.csdn.net/article/64/article/69/article/68/article/68/article/68/68424.shtm 那天发完帖发现搞错了...点修改..就那么改了...第二次想改的时候发现不行了...这事你还得问c#dn... 数据库连接出错!(急)! php,数据库问题 我的论坛www.hengsim.com 关于用php做web service的服务器端? php 开发工具使用问题 php网站项目找私人合作 如何判断当前的字符串是UTF-8还是GBK或GB2312 HELP ME! 郁闷的session问题!! 有人在osx系统下安装过shopnc吗? 【求助】一段PHP访客计数代码 PHP,缓存问题...
这里有个例子
不知道,这些是否可以帮到你?
以下为js代码(没找到js高亮标签)
//回调方法
function updatePage(){
//状态判断
if(xmlHttp.readyState == 4){
if (xmlHttp.status == 200){
var response = xmlHttp.responseText;
alert(response);
//404错误
}else if (xmlHttp.status == 404){
alert("Request URL does not exist");
//其他错误,显示状态码
}else{
alert("Error: status code is " + xmlHttp.status);
}
}
}
这里验证了,readyState必须要是4才能 alert关于firebug的调试,我看过header的信息,post里面是有值的,也传递过去了,但是php没有接收到,如果使用以下格式的代码,php可以接收到,并且返回正常,所以会很郁闷//获取子分类
function getSubCat(cat) {
//创建对象
createRequest(); var cid = "cid=1"; //设置服务器 URL
var url = "ajax.php";
//建立到服务器的新请求
xmlHttp.open("POST", url, true);
//告知服务器以text/xml方式接收
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//指定回调方法
xmlHttp.onreadystatechange = updatePage;
//向服务器发送请求
xmlHttp.send(cid);
}
可以用file_get_contents("php://input");来获取.
xmlHttp.send("parms="+ cid.toJSONString());然后php解析传过来的parms
==============
//告知服务器以text/xml方式接收
xmlHttp.setRequestHeader('Content-Type', 'text/xml');//soap方式。
php
==============
print_r( $GLOBALS['HTTP_RAW_POST_DATA']); //察看原始post数据
alert(response);
接受xml好像不是用responseText吧?再说。您为什么一定要用post提交呢?get传值不是很好吗?再来个md5()什么东东都OK拉
我还顺便vardump了一下,file_get_contents和$GLOBALS['HTTP_RAW_POST_DATA']的数据,都是以string的格式过来的..
刚才看手册的时候有这么一段,看样file_get_contents("php://input")效率会好一些php://input 允许读取 POST 的原始数据。和 $HTTP_RAW_POST_DATA 比起来,它给内存带来的压力较小,并且不需要任何特殊的 php.ini 设置。php://input 不能用于 enctype="multipart/form-data"。
关于你说的get传值,如果数据量小的话还好,但是如果是一个json格式的数组或者是一页的xml,这时url的长度就会出现问题,所以还是post比较好一些.. //设置服务器 URL(get方式传值)
var url = "ajax.php?cid="+escape(cid)+"&bid="+escape(bid)+.......;
关于xml的接收...正在折腾.
不过用
var response = xmlHttp.responseXML;
alert(response);
收不到格式化的值...
用xmlHttp.responseTEXT可以(这种格式只能是string)...
另外,
var response = xmlHttp.responseXML;
alert(response); 这个是不行的
xmlHttp.responseXML 返回的是xmlDOM对象,如果文件不xml文件,出错另外,xmlHttp.responseXML的默认属性不是字符串,所以alert(response); 出错多查查xmlhttp资料
数据当作xml返回时,php脚本少了一个header("Content-Type: text/xml; charset=utf8"); 怪不得接不到值,原来一直被当作string处理了...alert之后,提示的是 XML对象
附带一小段xml解析的方式,friebug调试通过了,我用for循环做的,不知道大家是怎么取值的???有没有更简单的方法?xml代码<paper>
<title>perl</title>
<title>php</title>
<title>js</title>
</paper>
js代码如下function updatePage(){
//状态判断
if(xmlHttp.readyState == 4){
if (xmlHttp.status == 200 ){
/*****************************
接收数据
responseText (string格式)
responseXML (xml object)
*****************************/
//alert(xmlHttp.responseText);
var xmlDoc = xmlHttp.responseXML.documentElement;
alert(xmlDoc.nodeName);
for (var x=0; x<xmlDoc.childNodes.length; x++) {
//获取 title 元素
var titleElement = xmlDoc.getElementsByTagName('title')[x];
//alert(titleElement); //debug
if(titleElement != null){
//获取第一个子元素
var titleText = titleElement.firstChild;
//输出值
alert(titleText.nodeValue);
}
}
//404错误
}else if (xmlHttp.status == 404){
alert("Request URL does not exist");
//其他错误,显示状态码
}else{
alert("Error: status code is " + xmlHttp.status);
}
}
}
附带一个js解析xml的DOM文档
http://dev.csdn.net/article/64/article/69/article/68/article/68/article/68/68424.shtm