我在将 XHR 响应类型设置为“json”时遇到问题。如果我将其保留为空字符串 xml.responseType = "";
,则效果很好,但是当我将其设置为“json”时,我收到控制台错误消息 SYNTAX_ERR:DOM Exception 12。
.js 文件:
var xml = new XMLHttpRequest();
xml.open("GET", "test.php", true);
xml.responseType = "json";
xml.send();
.php 文件:
<?php
$foo = "{\"key1\":\"val1\", \"key2\":\"val2\"}";
echo $foo;
?>
不确定发生了什么......有什么想法吗?
最佳答案
XMLHttpRequest
对象的 responseType
属性已添加到其新变体 XMLHttpRequest Level 2 中并且它包含在 HTML 5
中,我不确定所有浏览器都支持此方法,因此您可能使用的浏览器未实现该方法
您可以使用以下代码来获取所需格式的数据,而不是使用responseType
var xml = new XMLHttpRequest();
xml.open("GET", "test.php", true);
xml.onreadystatechange = function() {
if (xml.readyState != 4) { return; }
var serverResponse = JSON.parse(xml.responseText);
};
xml.send(null);
关于javascript - XMLHttpRequest responseType = "json"给出错误 SYNTAX_ERR : DOM Exception 12,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9845401/