XMLHttpRequest 到字符串 [英] XMLHttpRequest to string
问题描述
我正在尝试创建一个发送 XMLHttpRequest
并返回包含响应内容的字符串的函数,但它总是返回 null
.我该如何解决这个问题?
代码:
<预>函数 getPage() {如果(window.XMLHttpRequest){xmlhttp=新的 XMLHttpRequest();}别的{xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');}xmlhttp.open('GET','page.php',false);xmlhttp.send();xmlDoc = xmlhttp.responseXML;if ($.browser.msie) 返回 xmlDoc.xml;否则返回 (new XMLSerializer()).serializeToString(xmlDoc);}如果您使用的是 Internet Explorer,至少,那么您可能会收到空响应,因为 ContentType 标头 丢失或不正确.在 responseXML 属性上引用 Microsoft 的文档:
<块引用>如果...多用途互联网邮件扩展 (MIME) 类型不是正确设置为支持的之一MIME 类型...然后 responseXML 将空着.
MSXML 6.0 支持的 MIME 类型是:文本/xml"、应用程序/xml"或任何以+xml"结尾的,对于例如应用程序/rss+xml".
版本支持的 MIME 类型MSXML 6.0 之前的版本是:text/xml",应用程序/xml".
I'm trying to make a function that sends an XMLHttpRequest
and return a string with the contents of the response, but it always returns null
. How do I fix this?
Code:
function getPage() { if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); } xmlhttp.open('GET','page.php',false); xmlhttp.send(); xmlDoc = xmlhttp.responseXML; if ($.browser.msie) return xmlDoc.xml; else return (new XMLSerializer()).serializeToString(xmlDoc); }
If you are using Internet Explorer, at least, then you may have a null response because the ContentType header in the response is missing or incorrect. Quoting Microsoft's documentation on the responseXML property:
If the ... Multipurpose Internet Mail Extension (MIME) type was not correctly set to one of the supported MIME types ... then responseXML will be empty.
The supported MIME types for MSXML 6.0 are: "text/xml", "application/xml" or anything that ends with "+xml", for example "application/rss+xml".
The supported MIME types for versions prior to MSXML 6.0 are: "text/xml", "application/xml".
这篇关于XMLHttpRequest 到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!