I am trying my first AJAX and having a problem with my xml receiving function. I alert responseText and I can see the xml returned from my server, but when I try and get responseXML I get null and the error.
我正在尝试我的第一个AJAX并且我的xml接收功能有问题。我提醒responseText,我可以看到从我的服务器返回的xml,但是当我尝试获取responseXML时,我得到null和错误。
Here is the php function that builds my xml
这是构建我的xml的php函数
header('Content-type: application/xml');
echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
echo("<results>");
echo("<table><![CDATA[tablereererere]]></table>");
//echo("<ratedTable>".$_POST['ratedTable']."</ratedTable>\n");
//echo("<table>".$_POST['table']."</table>\n");
//echo("<post_id>".$_POST['post_id']."</post_id>\n");
//echo("<user_id>".$_POST['user_id']."</user_id>\n");
//echo("<rating>".$_POST['rating']."</rating>\n");
echo("</results>");
And here is my javascript function which is processing the returned xml
这是我的javascript函数,它处理返回的xml
function ajaxReceiver(http_request) {
//this function continues to run until a result is returned and then it creates the new div
if(http_request.readyState == 4) {
response_xml = http_request.responseXML;
response_text = http_request.responseText;
alert(response_text);
alert(response_xml.getElementsByTagName("table")[0].textContent);
//document.getElementById('floatingNotification').innerHTML = response_text;
// alert(http_request.responseXML.getElementsByTagName("table")[0].textContent);
//ratedTable = responseXML.getElementsByTagName("table").value;
//alert(ratedTable);
//message = response.getElementsByTagName('table')[0].textContent;
//alert(message);
//alert(message);
//this response contains the xml document that was returned by the php function.You can get any values out of the xml document and
//use javascript dom to manipulate the contents on the page
}
}
5 个解决方案
#1
2
It may be because, even though you're setting the content-type correctly, you need to have an <xml
tag in the top of your response. Also, you aren't closing your last tag properly. This should work:
这可能是因为,即使您正确设置了内容类型,也需要在响应顶部添加
echo("<?xml version='1.0'?>");
echo("<results>");
echo("<ratedTable>".$_POST['ratedTable']."</ratedTable>");
echo("<table>".$_POST['table']."</table>");
echo("<post_id>".$_POST['post_id']."</post_id>");
echo("<user_id>".$_POST['user_id']."</user_id>");
echo("<rating>".$_POST['rating']."</rating>");
echo("<message>$message</message>");
echo("</results>");
For more info on how to define your XML: http://www.w3.org/TR/REC-xml/#sec-prolog-dtd
有关如何定义XML的更多信息:http://www.w3.org/TR/REC-xml/#sec-prolog-dtd
#2
1
You are closing the parent node improperly (</results>
not <results/>
) and you should (after sanitizing it) wrap all the POSTDATA in <![CDATA[...]]>
tags to be safe. Make sure it's UTF8 encoded, too (see utf8_encode()
)
您正在不正确地关闭父节点(
而不是
EDIT: and what wajiw said about the <?xml version="1.0" encoding="UTF-8" ?>
tag at the beginning.
编辑:以及wajiw在开始时对 标记所说的内容。
Edit: Example CDATA Block Usage
<?xml version="1.0" encoding="UTF-8" ?>
<myNode>
<myData><![CDATA[
Now I just throw in my data, for fun and profit!
This way I can use special, reserved characters like <, > and &!
]]></myData>
</myNode>
Edit Yet Again:
Why not give Content-Type: text/xml
, NOT application/xml
, a go?
为什么不给Content-Type:text / xml,not application / xml,go?
#3
1
The answer is to put the asynchronous property in the function "open()" to false. Like this:
答案是将函数“open()”中的异步属性设置为false。喜欢这个:
ajaxObject.open("POST", "my_XML_Generator.php", false);
ajaxObject.setRequestHeader("Content-type", "text/xml");
ajaxObject.send();
#4
0
I had the same problem and couldn't solve it until I put my results into subnodes, e.g.
我遇到了同样的问题,直到我把结果放到子节点中才解决它,例如
header("Content-Type: text/xml; charset=utf-8");
echo("<?xml version='1.0' encoding='utf-8'?>\n");
echo("<summary>$summary</summary>\n");
echo("<content>$content</content>\n");
didn't work for me for some weird reason, but this does:
由于一些奇怪的原因,我没有为我工作,但这样做:
header("Content-Type: text/xml; charset=utf-8");
echo("<?xml version='1.0' encoding='utf-8'?>\n");
echo("<page>\n");
echo(" <summary>$summary</summary>\n");
echo(" <content>$content</content>\n");
echo("</page>\n");
My code retrieving the answer is
我的代码检索答案是
function retrieveRequest(title)
{
if (_xmlRequest.readyState == 4 && _xmlRequest.status == 200)
{
var xmlResponse = _xmlRequest.responseXML;
_divSummary.innerHTML = xmlResponse.getElementsByTagName("summary")[0].textContent;
_divContent.innerHTML = xmlResponse.getElementsByTagName("content")[0].textContent;
}
}
#5
0
I had this error happen to our team once and it took me a long time before we realized that the problem was in our XML data returned from the server. In particular, PHP script that generated return XML string was the culprit.
我有一次这个错误发生在我们的团队之前,我花了很长时间才意识到问题出在我们从服务器返回的XML数据中。特别是,生成返回XML字符串的PHP脚本是罪魁祸首。
My solution was to remove any white space from the beginning of the PHP script. I mean any spaces, new lines, and/or tabs need to be removed from the beginning of the script, so that the first thing in the script is the <?php
tag itself. It turned out that the <?PHP
tag was not the first thing on the first line of my PHP script; somehow I started my code on the second line and the first script line was simply an empty one.
我的解决方案是从PHP脚本的开头删除任何空格。我的意思是需要从脚本的开头删除任何空格,换行和/或制表符,这样脚本中的第一件事就是
It drove me mad and it took some time for me to figure this out, so I hope someone else can benefit from this solution. It is very simple and easy to try if all else fails.
它让我很生气,我花了一些时间才弄清楚这一点,所以我希望其他人可以从这个解决方案中受益。如果所有其他方法都失败,那么它非常简单易行。
#1
2
It may be because, even though you're setting the content-type correctly, you need to have an <xml
tag in the top of your response. Also, you aren't closing your last tag properly. This should work:
这可能是因为,即使您正确设置了内容类型,也需要在响应顶部添加
echo("<?xml version='1.0'?>");
echo("<results>");
echo("<ratedTable>".$_POST['ratedTable']."</ratedTable>");
echo("<table>".$_POST['table']."</table>");
echo("<post_id>".$_POST['post_id']."</post_id>");
echo("<user_id>".$_POST['user_id']."</user_id>");
echo("<rating>".$_POST['rating']."</rating>");
echo("<message>$message</message>");
echo("</results>");
For more info on how to define your XML: http://www.w3.org/TR/REC-xml/#sec-prolog-dtd
有关如何定义XML的更多信息:http://www.w3.org/TR/REC-xml/#sec-prolog-dtd
#2
1
You are closing the parent node improperly (</results>
not <results/>
) and you should (after sanitizing it) wrap all the POSTDATA in <![CDATA[...]]>
tags to be safe. Make sure it's UTF8 encoded, too (see utf8_encode()
)
您正在不正确地关闭父节点(
而不是
EDIT: and what wajiw said about the <?xml version="1.0" encoding="UTF-8" ?>
tag at the beginning.
编辑:以及wajiw在开始时对 标记所说的内容。
Edit: Example CDATA Block Usage
<?xml version="1.0" encoding="UTF-8" ?>
<myNode>
<myData><![CDATA[
Now I just throw in my data, for fun and profit!
This way I can use special, reserved characters like <, > and &!
]]></myData>
</myNode>
Edit Yet Again:
Why not give Content-Type: text/xml
, NOT application/xml
, a go?
为什么不给Content-Type:text / xml,not application / xml,go?
#3
1
The answer is to put the asynchronous property in the function "open()" to false. Like this:
答案是将函数“open()”中的异步属性设置为false。喜欢这个:
ajaxObject.open("POST", "my_XML_Generator.php", false);
ajaxObject.setRequestHeader("Content-type", "text/xml");
ajaxObject.send();
#4
0
I had the same problem and couldn't solve it until I put my results into subnodes, e.g.
我遇到了同样的问题,直到我把结果放到子节点中才解决它,例如
header("Content-Type: text/xml; charset=utf-8");
echo("<?xml version='1.0' encoding='utf-8'?>\n");
echo("<summary>$summary</summary>\n");
echo("<content>$content</content>\n");
didn't work for me for some weird reason, but this does:
由于一些奇怪的原因,我没有为我工作,但这样做:
header("Content-Type: text/xml; charset=utf-8");
echo("<?xml version='1.0' encoding='utf-8'?>\n");
echo("<page>\n");
echo(" <summary>$summary</summary>\n");
echo(" <content>$content</content>\n");
echo("</page>\n");
My code retrieving the answer is
我的代码检索答案是
function retrieveRequest(title)
{
if (_xmlRequest.readyState == 4 && _xmlRequest.status == 200)
{
var xmlResponse = _xmlRequest.responseXML;
_divSummary.innerHTML = xmlResponse.getElementsByTagName("summary")[0].textContent;
_divContent.innerHTML = xmlResponse.getElementsByTagName("content")[0].textContent;
}
}
#5
0
I had this error happen to our team once and it took me a long time before we realized that the problem was in our XML data returned from the server. In particular, PHP script that generated return XML string was the culprit.
我有一次这个错误发生在我们的团队之前,我花了很长时间才意识到问题出在我们从服务器返回的XML数据中。特别是,生成返回XML字符串的PHP脚本是罪魁祸首。
My solution was to remove any white space from the beginning of the PHP script. I mean any spaces, new lines, and/or tabs need to be removed from the beginning of the script, so that the first thing in the script is the <?php
tag itself. It turned out that the <?PHP
tag was not the first thing on the first line of my PHP script; somehow I started my code on the second line and the first script line was simply an empty one.
我的解决方案是从PHP脚本的开头删除任何空格。我的意思是需要从脚本的开头删除任何空格,换行和/或制表符,这样脚本中的第一件事就是
It drove me mad and it took some time for me to figure this out, so I hope someone else can benefit from this solution. It is very simple and easy to try if all else fails.
它让我很生气,我花了一些时间才弄清楚这一点,所以我希望其他人可以从这个解决方案中受益。如果所有其他方法都失败,那么它非常简单易行。