ajax中response与responseText的区别

  • 1.response与responseText的区别

1.response与responseText的区别

1.1、responseType属性:在客户端通过代码告诉请求代理对象,服务端响应给我们的数据类型。
MDN链接: responseType的值可参考该处

1.2、下面举个例子:

		var xhr = new XMLHttpRequest();
        xhr.open('GET', '15test.php');
        xhr.send();
        // 我们通过代码告诉请求代理对象,服务端响应给我们的是 JSON
        xhr.responseType = 'json';
        xhr.onreadystatechange = function () {
            if (this.readyState !== 4) return;
            console.log(this.response);
            // this.response 获取到的结果会根据 this.responseType 的变化而变
            // this.responseText 永远获取的是字符串形式的响应体
            //注意:在客户端这样设置responseType会有兼容问题,所以一般不这样写
        };

更多推荐

ajax中response与responseText的区别