RossGK RossGK - 4 months ago 13
Javascript Question

POST without response in Django/Javascript interaction

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.

Answer

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