1

If i do ajax call for form submit i can set header before sending request to server. But lets say i am hitting an action "url" to send data to server. In that case how to set any header for that request?

case1 :

<form onsubmit="somefn()">
<input type="text">
<input type="file">
<input type="submit">
</form>

In case 1 i can make an ajax call in "somefn()" and set request headers and send the form data.

case2:(For IE9 issue)

 <form action="/someurl">
    <input type="text">
    <input type="file">
    <input type="submit">
    </form>

Here in cas2 form will be submitted to the specified action URL. In this scenario how to set headers for that request? Because here we are not doing any ajax call.Its normal form submit

Actual scenario is file upload in IE 9 using "iframe" submit. But i need to send a "authtoken" along with the request . Other browser we are doing http request. So can set the "authtoken" easily.

How to achieve it. Please help

Subhadeep
  • 257
  • 5
  • 13
  • You have to use jQuery for this. Use ajax call and set header there. – Shubham Nov 25 '16 at 07:25
  • For submitting form data in IE9 , i have to do a normal form submit. Thats the only way i could find as of now because IE9 doesn't support file API. So in an iframe a form has to be submitted to send form data. I cant use jquery. – Subhadeep Nov 25 '16 at 07:33

1 Answers1

4

No.

There is no browser-side facility for setting arbitrary HTTP Request Headers when submitting a form, following a link, loading an image or any time other than using the XMLHttpRequest and fetch APIs.


Your options include:

  • Submit to a server side process you control and proxy the request, adding the header on your server.
  • Change your API so it uses a piece of standard form data instead of an HTTP header.
  • Tell people to stop using IE9. It has been almost a year since IE9 reached End Of Life. It is not supported by Microsoft. It does not receive security updates. People who use it expose themselves to higher levels of risk of malware infection.
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335