结合使用jQuery和JSON和AJAX responseText? [英] Using jQuery and JSON with AJAX responseText?

查看:122
本文介绍了结合使用jQuery和JSON和AJAX responseText?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,关于jQuery和json,我有点新.如果我使用json作为返回类型,是否仍可以从XMLHttpRequest对象检索responseText?

Ok, I'm a bit new when it comes to jQuery and json. If I'm using json as my return type, can I still retrieve responseText from an XMLHttpRequest object?

这是我正在使用的代码:

here's the code that i'm using:

json response: {"clients": []}

$.ajax({
        type: "POST",
        url: "/myurl/whatever.php",
        data: myData,
        dataType: "json",

        success: function(msg){
            status.html(msg[0]);
        },
        error: function(msg) {
                status.html("Error: " + msg[0]);
        }

        });

如果我想输出json响应或缺少某些信息,使用msg [0]是否正确?

is the use of msg[0] correct if i want to output the json response or am i missing something?

我如何仍将上述代码与XMLHttpRequest一起使用以获取状态,responseText等.

how can i still use the above code with XMLHttpRequest to get the status, responseText, etc.

谢谢!

推荐答案

据我所知,对$ .ajax的调用返回一个XHR对象,并且可以从其中提取responseText,例如:

As far as I know, the call to $.ajax returns a XHR object, and from that the responseText can be extracted, e.g.:

var xhr = $.ajax( {
                        url:' someInfo.php',
                        data: 'which=squirrels',
                        asynch: true
                } );

var resp = xhr.responseText;

响应文本将包含一个json字符串,该字符串需要转换为任何用途的对象.

The response text will contain a json string, which would need to be converted to an object to be of any use.

如果您想直接在success:函数中将响应用作json对象,请按照@cloudhead的建议进行操作,并使用 msg .您的选项中的dataType:"json"会为您完成转换.

If you want to use the response as a json object directly within your success: function, do as @cloudhead suggested, and use msg. The dataType : "json" in your options takes care of the conversion for you.

这篇关于结合使用jQuery和JSON和AJAX responseText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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