简体   繁体   English

jQuery.ajax,成功:函数(数据)不返回plainObject

[英]Jquery.ajax, success: function(data) NOT returning a plainObject

According to the documentation of Jquery.ajax success is a delegate (well at least I think it is, It lets you define the function to call) which will call a function and pass it a 根据Jquery.ajax的文档,成功是一个委托(至少我认为是这样,它可以让您定义要调用的函数),它将调用一个函数并将其传递给

PlainObject, String, jqXHR

But when I use the following code I am getting that that xml is NOT a PlainObject when I use alert to check. 但是,当我使用以下代码时,我发现使用Alert检查时xml不是PlainObject。

$(document).ready(function () {
    $("#dvContent").append("<ul></ul>");
    $.ajax({
        type: "GET",
        url: "message.xml",
        dataType: "xml",
        success: function (xml) {
            alert(jQuery.isPlainObject(xml));
            alert(jQuery.isPlainObject($(xml)));
            $(xml).find('Book').each(function () {

                var sTitle = $(this).find('Title').text();
                var sPublisher = $(this).find('Publisher').text();
            });
        },
        error: function () {
            alert("An error occurred while processing XML file.");
        }
    });
});

How is this possible? 这怎么可能?

That's the nature of a dynamic language. 这就是动态语言的本质。 Even though the documentation suggests that it will be PlainObject , this is not enforceable by the language. 即使文档建议使用它将是PlainObject ,但该语言也无法强制执行。 So because the content type which came back was xml and not something that can evaluated as a PlainObject . 因此,因为返回的内容类型是xml而不是可以评估为PlainObject

The following question details a bit more about the isPlainObject function: IsPlainObject, thing? 以下问题详细介绍了isPlainObject函数: IsPlainObject,是吗?

From the jQuery documentation, the crucial bit is that it will be 从jQuery文档中,关键的一点是它将

success: 成功:

Type: Function( PlainObject data, String textStatus, jqXHR jqXHR ) 类型:函数(PlainObject data,String textStatus,jqXHR jqXHR)

The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter ;... 该函数将传递三个参数:服务器返回的数据, 根据dataType参数设置格式 ; ...

So by saying describing it in the method signature as PlainObject , they are saying that (if possible) it will be evaluated into a real javascript object, otherwise, you're on your own and it's the raw response from the server. 因此,通过说在方法签名中将其描述为PlainObject ,他们说(如果可能的话)它将被评估为一个真正的javascript对象,否则,您将独自一人,这是服务器的原始响应。

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

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