jQuery在提交的数据中附加一些奇怪的字符串 [英] jQuery append some weird string into the data submitted

查看:71
本文介绍了jQuery在提交的数据中附加一些奇怪的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重现错误的代码(将以下代码放在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中的错误这是jQuery中的一个错误,特别是<一个href =http://bugs.jquery.com/ticket/8417 =nofollow> bug#8417 ,大约五周前被报道,错误地关闭,然后重新打开,现在有一个拉要求修复待处理;希望修复程序将成为未来版本。

Looks like a bug in jQuery to me 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.

下面的解决方法,但首先:此代码将其隔离复制:

Workaround below, but first: This code replicates it in isolation:

jQuery(function($) {

  $('#theButton').click(function go() {
    var data = { "Name": "test??", "testDesc": "description"};
    $.ajax({ 
      url: "/icece5", 
      data: JSON.stringify(data),
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      cache: false
    });
  });
});

实时拷贝

虽然我们无法在jsbin.com上看到POST的结果,但我们并不关心,因为这就是我们的'重新发送到我们担心的服务器。如果我发送并查看Chrome开发工具中的请求,我会清楚地看到数据被下面描述的JSON-P回调名称损坏。

Although we can't see the result of the POST on jsbin.com, we don't care, because it's what we're sending to the server that we're worried about. If I send that and look at the request in Chrome's dev tools, I clearly see the data being corrupted with the JSON-P callback name described below.

有趣的是,它没有如果我们不告诉jQuery我们期望从服务器返回JSON(请注意注释掉的行):

Interestingly, it doesn't happen if we don't tell jQuery we're expecting JSON back from the server (note the commented out line):

jQuery(function($) {

  $('#theButton').click(function go() {
    var data = { "Name": "test??", "testDesc": "description"};
    $.ajax({ 
      url: "/icece5", 
      data: JSON.stringify(data),
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      //dataType: "json", 
      cache: false
    });
  });
});

实时副本

返回的数据类型不应影响它对我们发送的数据的作用,我不会思想。但它确实邀请了一个解决方法:

The type of the data being returned shouldn't affect what it does with the data we're sending, I wouldn't have thought. But it does invite a workaround:

var data = { "Name": "test??", "testDesc": "testdesc??"};
$.ajax({ 
    url: "submit.aspx/SubmitData", 
    data: JSON.stringify(data),
    type: "POST", 
    contentType: "application/json; charset=utf-8", 
    dataType: "text",              // <== Tell jQuery you expect plain text
    cache: false
}).success(function (data) {
    data = jQuery.parseJSON(data); // <=== Then parse it yourself
});

建议将测试用例放在jsFiddle.net和向jQuery团队报告,因为你是那个有真实问题的人。如果它真的是一个bug,他们会修复它;如果没有,我有兴趣知道为什么不...

Recommend putting together a test case on jsFiddle.net and reporting it to the jQuery team, since you're the person with the real-life problem with it. If it's really a bug, they'll fix it; if not, I'd be interested to know why not...

原始答案

您引用的代码是执行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 <的使用/ code>等等。如果你切换到非缩小版本进行调试,你也可以在你使用的任何调试器中设置一个断点(你使用一个,对吗?没有理由不在第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在提交的数据中附加一些奇怪的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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