在 jQuery 中将 processData 设置为 false 会破坏我的 AJAX 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6253256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:35:53  来源:igfitidea点击:

Setting processData to false in jQuery breaks my AJAX request

ajaxjsonjquery

提问by paz

I've googled for a while now and can only find what processData: falsedoes. I can't find anyone who has experienced this same issue.

我已经用谷歌搜索了一段时间,只能找到什么processData: false。我找不到任何遇到过同样问题的人。

I'm passing JSON back to the server and do not want jQuery to automatically convert the data to a query string so I'm setting processData to false. I can see the request firing if I take out processData, but as soon as I put it in I do not see any requests being made (using Firebug & Chrome dev tools).

我将 JSON 传递回服务器,并且不希望 jQuery 自动将数据转换为查询字符串,因此我将 processData 设置为 false。如果我取出 processData,我可以看到请求被触发,但是一旦我把它放进去,我就看不到任何请求(使用 Firebug 和 Chrome 开发工具)。

$.ajax({
            url: myUrl,
            type: "POST",
            data: {foo: "bar"},
            processData: false,
            contentType: 'application/json'
        });

The request I was initially making was a bit more complex than this but I've simplified it to try to narrow down the problem but this simple piece of code does not work either (again, it does work if I comment out processData). Also, I am not seeing any JavaScript errors in the console.

我最初提出的请求比这更复杂一些,但我已经简化了它以尝试缩小问题的范围,但是这段简单的代码也不起作用(同样,如果我注释掉 processData,它确实起作用)。此外,我在控制台中没有看到任何 JavaScript 错误。

Edit

编辑

For future web searchers: As lonesomeday pointed out, jQuery will not throw any errors if you supply either a JS object or an incorrectly formatted JSON string. It will simply not fire the request.

对于未来的网络搜索者:正如 lonesomeday 指出的那样,如果您提供 JS 对象或格式不正确的 JSON 字符串,jQuery 不会抛出任何错误。它不会触发请求。

回答by lonesomeday

You want to pass the data as JSON. You are passing a Javascript object. JSON is a way of serializing Javascript objects to strings so that they can be passed around without compatibility issues.

您想将数据作为 JSON 传递。您正在传递一个 Javascript 对象。JSON 是一种将 Javascript 对象序列化为字符串的方法,以便它们可以在没有兼容性问题的情况下传递。

You actually want to pass the JSON in a string:

您实际上想在字符串中传递 JSON:

$.ajax({
    url: myUrl,
    type: "POST",
    data: '{"foo": "bar"}',
    processData: false,
    contentType: 'application/json'
});

回答by Paul T. Rawkeen

Actually, processDataby default assumes that datapassed is an object and sends it as application/x-www-form-urlencoded.

实际上,processData默认情况下假定data传递的是一个对象并将其作为application/x-www-form-urlencoded.

Summing up everything said above by @lonesomedayand @vsmto send raw JSON (what is different from the form data) you need to:

总结@lonesomeday@vsm上面所说的所有内容,以发送原始JSON(与表单数据不同的地方)您需要:

$.ajax({
    url: 'http://here-i.am/send-me/an/angel', // Determining far end
    data: JSON.stringify({foo: "bar"}), // Obtaining proper JSON string from data object
    processData: false, // Preventing default data parse behavior
    contentType: "application/json" // Setting proper `ContentType` for our data
    ...
});

回答by Gerard ONeill

Figure I'd add my current understanding (as of a few hours..)

图我会添加我目前的理解(截至几个小时..)

  • ProcessData = true : convert an object's name value pairs into a URL encoding, or an array's objects into name value pairs, or take a string as a literal.
  • ProcessData = false: take a string as a literal, or call an object's ToString() method.
  • ProcessData = true :将对象的名称值对转换为 URL 编码,或将数组的对象转换为名称值对,或将字符串作为文字。
  • ProcessData = false:将字符串作为字面量,或调用对象的 ToString() 方法。

On top of the ProcessData = true, by setting the 'traditional' flag, it can send it using a recursive encoding that captures complex structures, or a flat name value pair list.

在 ProcessData = true 之上,通过设置“传统”标志,它可以使用捕获复杂结构的递归编码或平面名称值对列表发送它。

So with respect to the OP, it worked without specifying processData since the default is true. So it converted the name value pairs in the object to a URLEncoded form. When you add the line back in, it calls your object's toString() method. Since you don't have a URL encoded string being returned by a toString() method (you have none), you will get a string such as "[object Object]". Perhaps jQuery cannot send strings that aren't URL encoded, or does not use the inherited toString() method.

因此,对于 OP,它在没有指定 processData 的情况下工作,因为默认值为 true。因此它将对象中的名称值对转换为 URLEncoded 形式。当您重新添加该行时,它会调用您对象的 toString() 方法。由于您没有由 toString() 方法返回的 URL 编码字符串(您没有),您将获得一个字符串,例如“[object Object]”。也许 jQuery 不能发送不是 URL 编码的字符串,或者不使用继承的 toString() 方法。

The two solutions presented convert the object to a JSON string, and thus there is no processing, and thus processData does nothing. The contentType setting helps the server understand what is being sent.

提供的两种解决方案将对象转换为 JSON 字符串,因此没有处理,因此 processData 什么也不做。contentType 设置可帮助服务器了解正在发送的内容。

In addition, one person commented that processing adds the encoded properties to the URL. Not quite: It sends that data via the most appropriate method; GET means appended to the URL, and POST means a urlencoded http body.

此外,有人评论说处理会将编码的属性添加到 URL。不完全是:它通过最合适的方法发送数据;GET 表示附加到 URL,POST 表示 URL 编码的 http 正文。