Javascript不能解释返回的xml
Javascript can't interpret returned xml
似乎我的javascript没有在我的php发送回xml文档。php代码:
$domtree = new DOMDocument('1.0', 'UTF-8');
/* append it to the document created */
$xmlRoot = $domtree->appendChild($domtree->createElement("root"));
foreach (glob('./img/photos/*.*') as $filename) {
//echo $filename;
$xmlRoot->appendChild($domtree->createElement("image",$filename));
}
/* get the xml printed */
echo $domtree->saveXML();
以上代码的输出如下:
<?xml version="1.0" encoding="UTF-8"?>
<root><image>./img/photos/2012-02-26 17.02.12.jpg</image>
<image>./img/photos/2012-03-09 08.21.48.jpg</image>
<image>./img/photos/2012-07-21 14.09.39.jpg</image>
<image>./img/photos/2012-07-25 15.25.17.jpg</image>
<image>./img/photos/2012-08-04 17.54.38.jpg</image>
<image>./img/photos/2012-08-04 23.36.30.jpg</image>
<image>./img/photos/2012-08-06 06.08.43.jpg</image>
<image>./img/photos/2012-08-07 20.57.34.jpg</image>
<image>./img/photos/2012-08-09 20.40.11.jpg</image>
<image>./img/photos/2012-08-25 20.54.05.jpg</image>
<image>./img/photos/2012-09-07 11.19.50.jpg</image>
<image>./img/photos/2012-09-08 15.53.27.jpg</image>
<image>./img/photos/2013-01-30 19.19.16.jpg</image>
<image>./img/photos/2013-01-31 09.48.39.jpg</image></root>
调用这个与AJAX,当我调用AJAXRequest。
编辑:AJAX请求代码:
function requestImages()
{
request=new XMLHttpRequest();
request.open("GET", "getPhotos.php");
request.onreadystatechange=showPhotos;
request.send();
}
function showPhotos()
{
if ((request.readyState == 4)) {
doc=request.responseXML; // This returns null
}
}
尝试在php中发送内容类型,以便AJAX知道这是xml并解析它(记住这必须在任何echo
之前完成):
header("Content-Type: text/xml");
这也可以在javascript中直接强制(使用overrideMimeType()
),但在php中更好。
如果您没有使用任何第三方库,请尝试这种代码片段(根据您的需求修改它)
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request.responseText, request.status);
}
};
request.open('GET', url, true);
request.send(null);
注意它使用了request。responseText,它肯定会得到返回的xml如果你在标题
中添加内容类型为header("Content-type: text/xml");
要解析xml,可以使用下面的代码片段:
function parseXml(str) {
if (window.ActiveXObject) {
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.loadXML(str);
return doc;
} else if (window.DOMParser) {
return (new DOMParser).parseFromString(str, 'text/xml');
}
}
function doNothing() {} //use this for some processing at run time
相关文章:
- 将ActiveXObject返回值转换为JQuery Object以处理XML
- IE8DOM转换XML,并且不从jQueryfind()或filter()返回任何内容
- JQuery AJAX 在 GET 请求完成时返回 XML
- 在 NodeJS + AJAX&XML 上返回成功
- 如何在node.js中返回与URL请求相对应的XML文件
- Web API在JSON调用上返回XML
- Ajax调用在本地返回json,但在服务器上运行时返回xml
- 异步xml请求不会;t返回
- 如何解析从 url 返回的此 xml
- 当标签值与使用 JavaScript 的输入匹配时,返回 XML 标签属性
- 我的 gulp 插件带有 through2 以奇怪的类似 XML 格式返回文件
- 编写从 XML 文件返回数据的搜索字段脚本
- Internet Explorer 11 不会读取 XML 的“getElementById”并返回 null
- ajaForm 在 responseXML 中返回无效的 XML
- 返回通过 jQuery 解析的 XML 数据
- AngularJS在从XML转换后不会返回JSON
- 删除XML字符串中的返回值
- jQuery XML解析-返回子元素的父元素
- JavaScriptAJAX调用一个返回XML的PHP
- nodejs使用cheerio解析xml返回空CDATA