Django/Javascript交互中没有响应的POST [英] POST without response in Django/Javascript interaction

查看:161
本文介绍了Django/Javascript交互中没有响应的POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个快乐的基于javascript的客户端,可与我的Django服务器端代码进行通讯.我使用几个XMLHttpRequests进行交互,一切都很好.现在,我想在服务器上进行一次用户交互,但是我不需要/想要响应.

I have a happy javascript based client that talks to my Django server-side code. I use several XMLHttpRequests for interactions and everything is fine there. Now there is one user interaction where I'd like to receive user-sent info at the server, but I don't need/want a response.

据我了解,Django所有视图都必须使用HttpResponse项进行响应.我可以发回一个空白的响应或HttpResponseNotModified响应.但是我收到的任何内容似乎都提示浏览器端更改为黑屏.

As I understand Django all views MUST respond with an HttpResponse item. I can send back a blank one, or a HttpResponseNotModified response. But anything I receive back seems to prompt the browser side to change to a blank screen.

我不想更改页面,重新加载或其他任何内容.只需不考虑浏览器会话.我不需要确认,也不介意在大量响应中甚至丢失了一些POST发送.

I don't want to change pages, reload or anything. Just leave the browser session alone. I don't need a confirmation, and don't mind if some of the POST sends are even lost among a large population of responses.

我可以执行XMLHttpResponse并捕获响应并忽略它.但是我想知道是否有解决办法.

I COULD do an XMLHttpResponse and catch the response and ignore it. But I'm wondering if there is any way around that.

推荐答案

您应该返回Http204/No Content,它告诉浏览器您的服务器已确认并处理了您的请求,但未返回任何内容并在您的请求中适当地处理了该请求.应用程序(无论您要如何).

You should return a Http204/No Content, which tells the browser that your server acknowledged and processed your request but it didn't return anything and handle that appropriately in your application (however you want to).

例如,如果您使用的是jQuery,它将自动转到您的success处理程序,并且如果您无法控制它,则将尽力而为.

If you're using jQuery (for example), then this will automatically go to your success handler, and if you have no control over that, you've done the best you could.

此处

这篇关于Django/Javascript交互中没有响应的POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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