简体   繁体   English

如何从jQuery中返回的对象中弄清楚

[英]How to make sense out of an object returned in jQuery

I have this jQuery code: 我有这个jQuery代码:

        $.ajax({
                type: "POST",
                url: "/problems/vote.php",
                dataType: "json",
                data: dataString,
                success: function(data)
                {           
                    // ? :)
                    alert (dataString);


                },
                error : function(data) 
                {
                    alert("ajax error, json: " + data);

                    //for (var i = 0, l = json.length; i < l; ++i) 
                    //{
                    //  alert (json[i]);
                    //}
                }
            });

And when I use it, I get in the alert something cryptic like this: object XMLHTTPReqest 当我使用它时,我会在警报中得到一些神秘的东西: object XMLHTTPReqest

How do I actually get the value that was passed in from the AJAX? 我实际上如何获得从AJAX传递过来的值?

Thanks! 谢谢!

have you tried using the Firefox plugin called FireBug? 您是否尝试过使用名为FireBug的Firefox插件? That's the first place I start as it will show you the exact structure, and data, of the json object returned. 这是我开始的第一个地方,它将向您显示返回的json对象的确切结构和数据。

from there it's a simple matter of coding in the object names etc. 从那里开始,只需在对象名称等中进行编码即可。

In a couple ways: 有几种方式:

console.log(data); // allows you to view your object in a tree in the console

or 要么

alert(JSON.stringify(data)); // alerts a serialized string of the object

or in chrome/safari and firefox (firebug) set a break point and check it out. 或在chrome / safari和firefox(萤火虫)中设置一个断点并进行检查。

Firefox - Download Firebug. Firefox-下载Firebug。 A must for any web dev. 任何Web开发人员都必须。

Chome: instead of alert, do "console.log(data);" Chome:执行“ console.log(data);”代替警报 This should spit out the object and you can view its properties in the console (hit F12 for that). 这应该吐出该对象,您可以在控制台中查看其属性(为此单击F12)。

Alternatively, write "debugger;" 或者,编写“ debugger;”。 right before your alert and the browser will pause execution. 在您发出警报之前,浏览器将暂停执行。 You need to have Firebug running or Chrome Dev Tools open. 您需要运行Firebug或打开Chrome开发工具。

First, you're alerting dataString , not anything returned from the server, at least in the success callback. 首先,至少在success回调中,您要警告dataString ,而不是从服务器返回的任何内容。 .ajax() callback signatures look like this: success(data, textStatus, jqXHR) , so if you're getting real data back, it can be accessed like any JSON object, via the data parameter. .ajax()回调签名如下所示: success(data, textStatus, jqXHR) ,因此,如果您要获取真实数据,则可以通过data参数像访问任何JSON对象一样对其进行访问。 In error it's error(jqXHR, textStatus, errorThrown) , so if that's what you're talking about, it kind of makes sense. error的是error(jqXHR, textStatus, errorThrown) ,所以如果这就是您error(jqXHR, textStatus, errorThrown) ,那是有道理的。

Use Firebug/Chrome/proxy/etc. 使用Firebug / Chrome / proxy / etc。 to make sure what you're getting back from the server is what you expect, or just log it to the console. 以确保您从服务器获得的收益是您所期望的,或者只是将其记录到控制台。

I assume you're talking about the error handler. 我假设您正在谈论错误处理程序。

The signature for the jQuery AJAX error handler is jQuery AJAX错误处理程序的签名是

error(jqXHR, textStatus, errorThrown) 错误(jqXHR,textStatus,errorThrown)

A function to be called if the request fails. 如果请求失败,将调用的函数。 The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred 该函数接收三个参数:jqXHR对象(在jQuery 1.4.x中为XMLHttpRequest),一个描述错误类型的字符串,以及一个可选的异常对象(如果发生)。

You cannot get the "data" returned as there is no data, just an error. 您将无法获得“数据”,因为没有数据,只是一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM