温馨提示×

Ajax中responseText解析json格式数据

小云
89
2023-09-01 13:54:53
栏目: 编程语言

在Ajax中,可以通过使用JSON.parse()方法将responseText解析为JSON格式的数据。

示例代码如下:

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = JSON.parse(this.responseText);
// 在这里可以使用解析后的JSON数据
console.log(response);
}
};
xmlhttp.open("GET", "url", true);
xmlhttp.send();

解析后的JSON数据可以通过response对象进行访问和使用。请注意,responseText应该是一个以JSON格式编码的字符串,否则解析将会失败。

0