从ajax请求中获取原始文本

Jon*_*an. 1 javascript jquery json local-storage

我使用ajax从服务器获取文件,该文件是JSON格式的字符串数组.当我执行如下的ajax请求时,jQuery会自动将JSON字符串解析为数组.

$.ajax({
    url: "modules.php",
    success: function (data) {
                //data is an array object not text
            }
    });
Run Code Online (Sandbox Code Playgroud)

但是我想将数据保存到localStorage,它只能保存字符串.我宁愿停止jQuery自动解析JSON字符串,保存文本然后自己解析JSON,而不是将数组(数据)转换回字符串.更像:

$.ajax({
    url: "modules.php",
    success: function (data) {
                //jQuery hasn't parsed the JSON already, and data is a string.
                localStorage.setItem("modules", data);
                var parsedData = $.parseJSON(data);
                //use the array
            }
    });
Run Code Online (Sandbox Code Playgroud)

ori*_*ori 6

根据文件

success: function (data, textStatus, jqXHR)
Run Code Online (Sandbox Code Playgroud)

XHR对象包含响应文本:

jqXHR.responseText
Run Code Online (Sandbox Code Playgroud)


Ant*_*mon 5

您需要使用以下dataType属性绕过 jQuery MIME 类型验证:

$.ajax({
    url: "modules.php",
    success: function (data) {
        //data is an array object not text
    },
    dataType: "html"
});
Run Code Online (Sandbox Code Playgroud)