Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery $.ajax custom http headers issue
    text
    copied!<p>I'm querying a REST webservice which uses custom http headers for authentication.</p> <p>If I perform a POST without the headers I'm getting the expected error, but when I add the headers I get a 404 error instead of what I actually need.</p> <p>This is my code</p> <pre><code>$.ajax({ type: 'POST', url: 'http://server.com/service', beforeSend: function (xhr) { xhr.setRequestHeader('CUSTOM-HEADER-KEY', 'CUSTOM-HEADER-VALUE') }, success: function(data) { alert('success.'); } }); </code></pre> <p>Here's the firebug headers output:</p> <blockquote> <p><strong>OPTIONS</strong> /service HTTP/1.1 Host: server.com User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,<em>/</em>;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Connection: keep-alive Origin: null Access-Control-Request-Method: POST <strong>Access-Control-Request-Headers: custom-header-key</strong> Pragma: no-cache Cache-Control: no-cache</p> </blockquote> <p>and the smae headers when performing the post with poster, which returns desired result.</p> <blockquote> <p><strong>POST</strong> /service HTTP/1.1 Host: server.com User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,<em>/</em>;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Connection: keep-alive <strong>CUSTOM-HEADER-KEY: CUSTOM-HEADER-VALUE</strong> Pragma: no-cache Cache-Control: no-cache Content-Length: 0</p> </blockquote> <p>The difference is pretty obvious, but I don't know what I'm doing wrong in the jquery code.</p> <p>Could anyone help me please?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload