在jQuery中将processData设置为false会中断我的AJAX请求 [英] Setting processData to false in jQuery breaks my AJAX request

查看:1091
本文介绍了在jQuery中将processData设置为false会中断我的AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了一段时间,只能找到processData: false的内容.我找不到遇到相同问题的人.

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

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

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).

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

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

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.

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

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.

推荐答案

您要将数据作为JSON传递.您正在传递Javascript对象. JSON是一种将Javascript对象序列化为字符串的方法,这样就可以传递它们而不会出现兼容性问题.

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:

You actually want to pass the JSON in a string:

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

这篇关于在jQuery中将processData设置为false会中断我的AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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