My understanding is that when this function is called, it will send the form data from a JSON model up to the server with reference to my URL.
My question is after the form has been sent to the server:
1) Why should the server have to respond to this post request with a response? Isn't this just a one way transaction? 2) If the server responds after an ajax call, how do you get this value from Backbone or ajax? The method call i'm using i believe does not return anything.
The Get method (model.fetch()) from backbone seems to make sense from the client side, you send the GET request and the server responds with data. Can someone talk about responses in general; why they are important, and how you should use them?
Thanks much.
Imagine how you'd feel about going to an e-commerce site, selecting an item, entering your credit card information, and hitting the submit button. The site then redirects you back to the home page, with no indication of what happened. Is your first thought "Well, no news is good news?" or "Oh dear, something went wrong?"
A response is important in any conversation, both to acknowledge receipt and give some indication of how the recipient did, or plans to, deal with the message. A well-constructed service method should respond with, at the very least, an HTTP 202 ACCEPTED
header on successful create/update/delete operations, and the HTTP protocol absolutely expects some kind of response to every request, even if the response has an empty payload and consists only of an HTTP status message.
So, to answer your questions: 1) The server must respond with something because otherwise, your browser will assume the HTTP connection failed. If it is a successful response, a status code in the 200 series is appropriate.
2) If you're using raw AJAX, you can inspect the value of the response in the callback -- see How can I take advantage of callback functions for asynchronous XMLHttpRequest? for an example. Backbone returns a jQuery XHR (jqXHR) object from save()
calls, so you can use that to inspect the value. http://api.jquery.com/jQuery.ajax/#jqXHR