XMLHtt prequest responseType =" JSON"给错误SYNTAX_ERR:DOM异常12 [英] XMLHttpRequest responseType = "json" giving error SYNTAX_ERR: DOM Exception 12

查看:3215
本文介绍了XMLHtt prequest responseType =" JSON"给错误SYNTAX_ERR:DOM异常12的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法设置XHR responseType为json的。它工作正常,如果我让一个空字符串 xml.responseType =; 但是当我将它设置为JSON我得到的控制台错误消息SYNTAX_ERR:DOM异常12

I am having trouble setting the XHR responseType to "json". It works fine if I leave it an empty string xml.responseType = ""; but when I set it to "json" I get the console error message SYNTAX_ERR: DOM Exception 12.

的.js文件:

var xml = new XMLHttpRequest();
xml.open("GET", "test.php", true);
xml.responseType = "json";
xml.send();

的PHP文件:

The .php file:

<?php
$foo = "{\"key1\":\"val1\", \"key2\":\"val2\"}";
echo $foo;
?>

不知道是怎么回事。任何想法?

Not sure what's going on.. Any ideas?

推荐答案

responseType 属性 XMLHtt prequest 对象在其新的变种 XMLHtt prequest 2级并包括在添加 HTML 5 ,我不知道所有的浏览器都支持这种方法,因此它可能是可能是你使用的是没有实现的方法,一个浏览器

responseType property for XMLHttpRequest object is added in its new variant XMLHttpRequest Level 2 and which is included in HTML 5, i am not sure all browsers support this method so it could be possible that you are using a browser which doesn't implement that method

responseType ,而不是用你可以用下面的code以获得数据所需的格式

instead of using responseType you can use following code to get data in desired format

 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);

这篇关于XMLHtt prequest responseType =&QUOT; JSON&QUOT;给错误SYNTAX_ERR:DOM异常12的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆