0

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?

Curtis
  • 101,612
  • 66
  • 270
  • 352

2 Answers2

1

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

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.

Pimp Trizkit
  • 19,142
  • 5
  • 25
  • 39
  • That values to encode the value (which might include special characters) to make it safe for inserting into a URI, and it sends the data in the URI instead of the request body (where it belongs in a POST request). – Quentin Oct 25 '12 at 16:12
  • "You could just use AJAX or XMLHttpRequest" is like saying "You could just drive or use a car". – Quentin Oct 25 '12 at 16:13
  • @Quentin - yes i know, but you can drive, or more specifically drive a car (not a truck). I was just saying that because i know there is also a jQuery way of doing it that actually uses ajax syntax... So, there are two ways. – Pimp Trizkit Oct 25 '12 at 16:14
  • @Quentin - And yes, again, there might be some encoding issues depending on the data being sent and the requirements of OP. But, with out changing the server, I dont know of another way. – Pimp Trizkit Oct 25 '12 at 16:18
  • 1
    [`encodeURICompoent`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent) – Quentin Oct 25 '12 at 16:21