POST without response in Django/Javascript interaction

佐手、 提交于 2020-01-17 00:40:27

问题


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.

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.

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.

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


回答1:


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

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.

Read the full list of status codes here



来源:https://stackoverflow.com/questions/17557618/post-without-response-in-django-javascript-interaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!