问题描述
重现错误的代码(将以下代码放在html页面中并读取Fiddler中提交的数据.jQuery 1.4.2工作正常,问题发生在1.5.1和1.5.2中):
Code to reproduce the bug (Put the following code in the html page and read the data submitted in Fiddler. jQuery 1.4.2 works fine, the problem happens in 1.5.1 and 1.5.2):
<html>
<head>
<script src="https://github.com/douglascrockford/JSON-js/raw/master/json2.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script>
var data = { "Name": "test??", "testDesc": "testdesc??"};
$.ajax({
url: "submit.aspx/SubmitData",
data: JSON.stringify(data),
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false
}).success(function (msg) {});
</script>
</head>
<body></body>
</html>
我使用jQuery 1.5.1进行数据处理并将数据提交给服务器
I am using jQuery 1.5.1 for data processing and submit data to the server
function test(name, testDesc){
var data = { "Name": name, "testDesc": testDesc};
$.ajax({
url: "submit.aspx/SubmitData",
data: JSON.stringify(data),
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false
}).success(function (msg) {//......});
}
在一千条记录中,有一些记录的字符串如'jQuery151023383707909744822_1301931324827'和数据中的'jQuery151033911434971578497_1301989384660'。
Among one thousand records, there are a few records with string like 'jQuery151023383707909744822_1301931324827' and 'jQuery151033911434971578497_1301989384660' in the data.
这是jQuery或导致问题的任何插件的错误吗?
Is that the bug of jQuery or any plugins that causes the problem?
更多信息:
奇怪的字符串似乎取代了原来的?在那些记录中。
例如,
你喜欢StackOverflowjQuery151023383707909744822_1301931324827我非常喜欢它。
这是goodjQuery151023383707909744822_1301931324827这很棒。
More information:The weird string seems replace the original "?" in those records.For example,Do you like StackOverflowjQuery151023383707909744822_1301931324827 I love it very much.Is this goodjQuery151023383707909744822_1301931324827 It's great.
错误更新:
我已经复制了一个bug的案例。如果我输入test ??对于名称,提交的数据变为{名称:testjQuery15103933552800185728_1302170988810,描述:fdsa}
Update for the bug:I have reproduced a case for the bug. If I input "test??" for the name, the submitted data becomes "{"Name":"testjQuery15103933552800185728_1302170988810","Description":"fdsa"}"
推荐答案
这是jQuery中的一个错误,特别是 bug#8417 ,大约五周前被报道,错误地关闭,然后重新打开,现在有一个拉要求修复待处理;希望修复程序将成为未来版本。
It is a bug in jQuery, specifically bug #8417, which was reported about five weeks ago, closed erroneously, then reopened and there's now a pull request with a fix pending; hopefully the fix will make a future version.
原始答案:
您引用的代码是执行JSON编码数据的 POST
,但是您提到的字符串( jQuery151023383707909744822_1301931324827
等)是jQueryexpando(在运行时通过获取jQuery版本并向其添加随机值来确定)加 _
加上一个纪元值。 jQuery以该形式创建字符串作为JSON-P回调函数的默认名称,如下所示:
The code you've quoted is doing a POST
of JSON-encoded data, but the strings you've mentioned (jQuery151023383707909744822_1301931324827
, etc.) are the jQuery "expando" (which is determined at runtime by taking the jQuery version and adding a random value to it) plus _
plus an epoch value. jQuery creates strings in that form as the default names of JSON-P callback functions, as seen here:
jQuery-1.5.1.js的第1,326-1,329行: / p>
Lines 1,326-1,329 of jQuery-1.5.1.js:
// Unique for each copy of jQuery on the page
// Non-digits removed to match rinlinejQuery
expando: "jQuery" + ( jQuery.fn.jquery + Math.random() ).replace( /\D/g, "" ),
和(第7,029-7,035行):
and (lines 7,029-7,035):
// Default jsonp settings
jQuery.ajaxSetup({
jsonp: "callback",
jsonpCallback: function() {
return jQuery.expando + "_" + ( jsc++ );
}
});
所以我的猜测是你引用的代码不是产生这些值的代码,而是您有其他代码使用它们。
So my guess is that the code you're quoting isn't the code generating these values, but that you have other code using them.
JSON-P回调的名称将替换?
中的如果您对请求使用JSON-P,请求的URL。我不希望它在您的数据中执行此操作。
The name of the JSON-P callback will replace the ?
in the URL of a request if you use JSON-P for the request. I wouldn't expect it to do that in your data.
我将审核您对 ajaxSettings 等等。如果你切换到非缩小版本进行调试,你也可以在你使用的任何调试器中设置一个断点(你使用一个,对吗?没有理由不在第7,033行(其中正在创建这些字符串)然后走出去找出你的代码正在触发它。
I'd review your use of ajaxSettings
and such. If you switch to the un-minified version for debugging, you can also set a breakpoint in whatever debugger you use (and you use one, right? there's no excuse not to) on line 7,033 (where those strings are being created) and then step out to find out what part of your code is triggering it.
这篇关于jQuery在提交的数据中附加一些奇怪的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!