对jQuery处理我的数据感到困惑 [英] Confused about jQuery handling of my data

查看:59
本文介绍了对jQuery处理我的数据感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很可能不是最好的标题,但这就是我想的.

So likely not the best title but that's what I think is going on.

我写了此问题,想填充一个下拉列表在我的jqGrid中,以便用户可以从可用选择中进行筛选.提供的解决方案仅在一种情况下有效,而在另一种情况下无效.它的第一个类型检索数据(用于列标题下拉过滤器),我的堆栈跟踪如下所示:

I wrote this question wanting to populate a dropdownlist in my jqGrid so the user could pick from the available selections for filtering. The solution provided works in one instance but not in another. The first type it retrieves the data (for the column header dropdown filter) my stack trace looks like so:

callback() jquery-1.6.2.js (line 7947)
_ = readystatechange

done() jquery-1.6.2.js (line 7183)
status = 200
statusText = "success"
responses = Object { text="["Cake", "Sugar", "Waffle"]" }
headers = "Server: ASP.NET Develop...: 22\nConnection: Close\n"

resolveWith() jquery-1.6.2.js (line 1008)
context = Object { url="/IceCream/AvailableConeTypes", isLocal=false, more...}
args = [ Object { readyState=4, responseTExt="["Cake", "Sugar", "Waffle"]", more...} "success"]

complete() jquery...src.js(line 3591)
res = Object { readyState=4, responseText="["Cake", "Sugar", "Waffle"]", more...}
status = "success"

myBuildSelect() Cone (line 75)
data = Object { readyState=4, responseText="["Cake", "Sugar", "Waffle"]", more...}

第二次调用此函数时(对于jqGrid工具栏过滤器对话框,该对话框允许您构造多个AND/OR过滤器),堆栈跟踪略有不同:

the second time when this is called (for the jqGrid toolbar filter dialog that allows you to construct multiple AND/OR filters) the stack trace is slightly different:

callback() jquery-1.6.2.js (line 7947)
_ = readystatechange

done() jquery-1.6.2.js (line 7168)
status = 200
statusText = "success"
responses = Object { text="["Cake", "Sugar", "Waffle"]" }
headers = "Server: ASP.NET Develop...: 22\nConnection: Close\n"

resolveWith() jquery-1.6.2.js (line 1008)
context = Object { elem=, options={...} }
args = [ "["Cake", "Sugar", "Waffle"]", "success", Object { readyState=4, responseText="["Cake", "Sugar", "Waffle"]", more...} ]

success() jquery...src.js(line 5099)
data ="["Cake", "Sugar", "Waffle"]"
status = "success"

myBuildSelect() Cone (line 75)
data = ="["Cake", "Sugar", "Waffle"]"

我对这里发生的事情感到困惑.查看引用行的jquery-1.6.2.js文件,我看到它是在第一个实例中执行以下行:

I'm confused as to what's going on here. Looking at the jquery-1.6.2.js file at the referenced lines, I see that in the first instance it is executes the line:

deferred.resolveWith { callbackContext, [success, statusText, jqXHR] );  (line 7168)

,并在第二个实例中执行以下行:

and in the second instance it executes the line:

completeDeferred.resolveWith( callbackContext, [ jqXHR, statusText] );  (line 7183)

当第二次调用该控制器动作时,似乎事物缓存或处理方式有所不同.如果有帮助,我可以发布其他代码,但这实际上是我在前面的问题中与Oleg的解决方案一起编写的.想法?

Seems like things are being cached or handled differently when the call is made to that controller action the second time. I can post additional code if that is helpful, but it is essentially what I wrote in my earlier question along with Oleg's solution. Ideas?

推荐答案

首先,我建议您使用jqGrid的最新版本4.1.2代替非常老的4.0.0版本(您包含在问题中的>来自v4.0.0).版本4.1.2包含许多错误修复.

First of all I would recommend you to use the last 4.1.2 version of jqGrid instead of very old 4.0.0 version (the line numbers of the jquery.jqGrid.src.js which you included in the question are from the v4.0.0). The version 4.1.2 includes many bug fixes.

以下是您遇到问题的原因.可以使用 jQuery.ajax complete或successerror事件处理程序处理服务器响应.旧的jqGrid代码到处都使用了complete事件处理程序.这不是最好的方法,因此在许多地方但不是到处都有(!!!),jqGrid代码已更改,并且大多不使用successerror处理程序.在complete处理程序内部,data参数的类型为string(在您的情况下为JSON格式).因此,需要另外调用 $ .parseJSON 将数据转换为对象.在success内部,已经处理过data,它对应于服务器响应的"Content-Type" HTTP标头和$.ajaxdataType参数.

The reason on the problem which you have is the following. One can use complete or success and error event handlers of the jQuery.ajax to process the server response. The old jqGrid code used the complete event handler everywhere. It was not the best way, so in many places but not everywhere (!!!) the jqGrid code was changed and not mostly success and error handlers are used. Inside of complete handler the data parameter has the string type (in JSON format in your case). So one need make additional call of $.parseJSON to convert the data to the object. Inside of success the data are already processed corresponds to the "Content-Type" HTTP header of the server response and the dataType parameter of the $.ajax.

可以在jqGrid的源代码中找到buildSelect将在

One can find in the source code of jqGrid, that the buildSelect will be called in filterToolbar inside of complete handler and it will be called in createEl inside of success handler. It's the problem which you has.

我建议您将问题的描述作为错误报告发布在 trirand论坛,或者我可以为您做.

I recommend you to post the description of the problem as the bug report in the trirand forum or I can do it for you.

作为解决方法,我建议修改buildSelect函数,该函数在

As the workaround I suggest to modify the buildSelect function, which I suggested in my answer on your previous question, to the following:

my.buildSelect = function(data) {
    var response = typeof(data) === "string" ?
                       jQuery.parseJSON(data.responseText):
                       data,
        s = '<select>', i, l, ri;

    if (response && response.length) {
        for (i=0, l=response.length; i<l; i += 1) {
            ri = response[i];
            s += '<option value="' + ri + '">' + ri + '</option>';
        }
    }
    return s + '</select>';
};

因此,我建议测试data输入参数的类型.更改之后,该代码应在所有情况下均有效,并且如果将jqGrid更改为在任何地方都使用success处理程序而不是complete处理程序,它将仍然有效.

So I suggest to test the type of the data input parameter. After the changes the code should work in all situations and it will still work if jqGrid will be changed to use the success handler instead of complete handler everywhere.

这篇关于对jQuery处理我的数据感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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