I have the following form:
<form method="post" action="http://domain.com/api.json?param=value"></form>
On submission of this form, this will replace my document with a response from http://domain.com/api.json
.
Is it possible to POST this form, but prevent receiving a response, and keep the existing HTML with the form
?
Send an HTTP 204 No Content response instead of the usual 200 OK response.
10.2.5 204 No Content
The server has fulfilled the request but does not need to return an
entity-body, and might want to return updated metainformation. The
response MAY include new or updated metainformation in the form of
entity-headers, which if present SHOULD be associated with the
requested variant.
If the client is a user agent, it SHOULD NOT change its document view
from that which caused the request to be sent. This response is
primarily intended to allow input for actions to take place without
causing a change to the user agent's active document view, although
any new or updated metainformation SHOULD be applied to the document
currently in the user agent's active view.
The 204 response MUST NOT include a message-body, and thus is always
terminated by the first empty line after the header fields.
You could just use AJAX (XMLHttpRequest in this example) to submit the post
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://domain.com/api.json?param=value", true);
xmlhttp.send(null);
Before hand, if you need. You can grab your param value and encode it:
var val = encodeURIComponent(document.getElementById("param").value);
then the second line would be more like:
xmlhttp.open("POST", "http://domain.com/api.json?param="+val, true);
Otherwise, any sort of submitting from a form
will load a page. A hack would be to put it in a iframe thats hidden, and just delete the iframe when done.