typeof(data) ajax,jQuery dataFilter以某種方式導致將空數據發送到ajax成功回調

I have defined an ajax dataFilter for my jQuery calls that return JSON data to ensure that .Net-encoded dates are automatically converted to JavaScript dates on return:

我為我的jQuery調用定義了一個ajax dataFilter,它返回JSON數據以確保.Net編碼的日期在返回時自動轉換為JavaScript日期:

$.ajaxSetup({

dataFilter: function(data, type) {

var rx = /"\\\/Date\(([0-9]+)\)\\\/"/g;

if(type == 'json' && typeof(data) == 'string' && rx.test(data)) {

var obj = JSON && JSON.parse ? JSON.parse(data) : eval(data);

replaceMicrosoftJSONDates(obj);

return obj;

}

return data;

}

});

The replaceMicrosoftJSONDates function works fine; it recursively iterates through the object and replaces strings matching the appropriate date format string with actual JavaScript dates, but here it is for testing purposes:

replaceMicrosoftJSONDates函數工作正常;它以遞歸方式遍歷對象,並將匹配相應日期格式字符串的字符串替換為實際的JavaScript日期,但此處用於測試目的:

function replaceMicrosoftJSONDates(obj) {

for(var p in obj)

switch(typeof(obj[p])) {

case 'string':

var match = /^\/Date\(([0-9]+)\)\/$/.exec(obj[p]);

if(match) obj[p] = new Date(parseInt(match[1]));

break;

case 'object':

replaceMicrosoftJSONDates(obj[p]);

break;

}

}

The problem is with the dataFilter function. If I return the modified JSON object, null is sent as the data parameter to the ajax success callback:

問題出在dataFilter函數上。如果我返回修改后的JSON對象,則將null作為數據參數發送到ajax成功回調:

$.ajax({

type: 'GET',

cache: false,

url: __appPath + 'path/to/my/page',

success: function(data) {

console.log(data); // displays null

}

});

As far as I can tell, the jQuery private function involved in this process is ajaxConvert, which you can see at https://github.com/jquery/jquery/blob/master/src/ajax.js#L897-979

據我所知,這個過程中涉及的jQuery私有函數是ajaxConvert,你可以在https://github.com/jquery/jquery/blob/master/src/ajax.js#L897-979看到它。

Keep in mind the following:

請記住以下內容:

This is not a cross-domain scripting call

這不是跨域腳本調用

The server is return an HTTP200 (OK) response and the responseText has the full data

服務器返回HTTP200(OK)響應,responseText具有完整數據

The data is being sent correctly to the dataFilter function

數據正在正確發送到dataFilter函數

The dataFilter function appears to correctly parsing and modifying the response and returning a valid JavaScript object, already parsed and ready to consume

dataFilter函數似乎正確地解析和修改響應並返回已經解析並准備使用的有效JavaScript對象

If I return a string from the dataFilter function, jQuery does its job correctly and sends me a parsed JSON object. If I return a pre-parsed JavaScript object, jQuery sends null to my success callback. I am using jQuery 1.5.2 in this case and this filter used to work in other applications and older versions of jQuery. Now all of a sudden it doesn't seem to work anymore.

如果我從dataFilter函數返回一個字符串,jQuery正確地完成它的工作並向我發送一個解析的JSON對象。如果我返回一個預先解析的JavaScript對象,jQuery會向我的成功回調發送null。我在這種情況下使用jQuery 1.5.2,這個過濾器曾用於其他應用程序和舊版本的jQuery。現在突然間它似乎不再起作用了。

SOLUTION

2 个解决方案

#1

3

Rick Strahl had problem similiar to this recently (this seems to be specific for jQuery 1.5.2). As I don't wan't to copy his work, here is his solution which should easly guide you to solve your problem: http://codepaste.net/i89xhc

Rick Strahl最近遇到了類似的問題(這似乎是jQuery 1.5.2特有的)。因為我不想復制他的作品,這是他的解決方案,應該很容易指導你解決你的問題:http://codepaste.net/i89xhc

#2

1

Just a quick comment from my experience :

只是根據我的經驗快速評論:

wherever you put dataFilter method for remote validation just ensure that it returns stringyfied boolean values sth like this:

無論你將dataFilter方法用於遠程驗證,只需確保它返回stringyfied布爾值,如下所示:

return $.toJSON(true)

or

return $.toJSON(false)

I lost couple of hours to find this :)

我失去了幾個小時找到這個:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值