jQuery的JSON的Ajax请求空请求体 [英] Empty request body on jQuery JSON ajax request

查看:338
本文介绍了jQuery的JSON的Ajax请求空请求体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器:Django的1.4在Heroku上运行。所有的请求是HTTPS。

Server: Django 1.4 running on Heroku. All requests are HTTPS.

我的Ajax调用是这样的:

My ajax calls look like this:

var data = {
  username: form.find('input[name="username"]').val(),
  email: form.find('input[name="email"]').val()
};
$.ajax({
  url: '/register',
  type: 'POST',
  data: JSON.stringify(data),
  contentType: 'application/json',
  dataType: 'text',
  headers: {
    'X-CSRFToken': $.cookie('csrftoken')
  }
});

有时候,解析请求主体服务器上的失败,因为 request.body 完全是空的。这是相当罕见的,但因为我用这个成语很多网页,我看到它每星期几次。大概在2-5%这些请求。

Occasionally, parsing the request body on the server fails because request.body is completely empty. It's fairly rare, but since I use this idiom on many pages, I see it a few times each week. Probably about 2-5% of these requests.

在CSRF令牌头有没有一起休息,所以似乎只有身体本身缺少。在最近的一个要求,我注意到一个 CONTENT-2 头为好,这将是太短,包含真正的请求数据。我不知道在哪里,进行了补充。

The CSRF token header is there along with the rest, so it appears that only the body itself is missing. In a recent request, I noticed a CONTENT_LENGTH 2 header as well, which would be too short to contain the real request data. I wonder where that got added.

我喜欢一些帮助调试这一点。我看不到任何方式为 JSON.stringify 调用返回空字符串。那是合理的?如果是这样,那么它必须是该请求主体要么是得到飞行剥离,或东西在我的Django应用程序在做。

I would love some help debugging this. I can't see any way for the JSON.stringify call to return the empty string. Is that reasonable? If so, then it has to be that the request body is either getting stripped off in-flight, or something in my Django application is doing it.

任何帮助调试或想法如何重现,这将是非常受欢迎的。

Any help debugging or ideas on how to reproduce this would be most welcome.

推荐答案

我见过类似的行为的唯一的一次是当我请求数据,并在检索最后我跨preT的数据的错误的类型,例如。在我的情况的jQuery预期JSON,但XML被送到然后我得到空数据。然后,我必须设置的jQuery明确除preT的数据的XML。难道是这样的吗?

The only time I've seen similar behavior is when I request data and on the retrieving end I interpret the data as the wrong type eg. in my case jQuery expects JSON but XML was sent then I get empty data. I then have to set jQuery to explicitly interpret the data as xml. Could it be the case for you?

这篇关于jQuery的JSON的Ajax请求空请求体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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