Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get a cross-origin resource sharing (CORS) post request working
    primarykey
    data
    text
    <p>I have a machine on my local lan (machineA) that has two web servers. The first is the in-built one in XBMC (on port 8080) and displays our library. The second server is a CherryPy python script (port 8081) that I am using to trigger a file conversion on demand. The file conversion is triggered by a AJAX POST request from the page served from the XBMC server.</p> <ul> <li>Goto <a href="http://machineA:8080" rel="noreferrer">http://machineA:8080</a> which displays library</li> <li>Library is displayed</li> <li>User clicks on 'convert' link which issues the following command - </li> </ul> <p>jQuery Ajax Request</p> <pre><code>$.post('http://machineA:8081', {file_url: 'asfd'}, function(d){console.log(d)}) </code></pre> <ul> <li>The browser issues a HTTP OPTIONS request with the following headers;</li> </ul> <p><strong>Request Header - OPTIONS</strong></p> <pre><code>Host: machineA:8081 User-Agent: ... Firefox/4.01 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;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 Keep-Alive: 115 Connection: keep-alive Origin: http://machineA:8080 Access-Control-Request-Method: POST Access-Control-Request-Headers: x-requested-with </code></pre> <ul> <li>The server responds with the following;</li> </ul> <p><strong>Response Header - OPTIONS (STATUS = 200 OK)</strong></p> <pre><code>Content-Length: 0 Access-Control-Allow-Headers: * Access-Control-Max-Age: 1728000 Server: CherryPy/3.2.0 Date: Thu, 21 Apr 2011 22:40:29 GMT Access-Control-Allow-Origin: * Access-Control-Allow-Methods: POST, GET, OPTIONS Content-Type: text/html;charset=ISO-8859-1 </code></pre> <ul> <li>The conversation then stops. The browser, should in theory, issue a POST request as the server responded with the correct (?) CORS headers (Access-Control-Allow-Origin: *)</li> </ul> <p>For troubleshooting, I have also issued the same $.post command from <a href="http://jquery.com" rel="noreferrer">http://jquery.com</a>. This is where I am stumped, from jquery.com, the post request works, a OPTIONS request is sent following by a POST. The headers from this transaction are below;</p> <p><strong>Request Header - OPTIONS</strong></p> <pre><code>Host: machineA:8081 User-Agent: ... Firefox/4.01 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;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 Keep-Alive: 115 Connection: keep-alive Origin: http://jquery.com Access-Control-Request-Method: POST </code></pre> <p><strong>Response Header - OPTIONS (STATUS = 200 OK)</strong></p> <pre><code>Content-Length: 0 Access-Control-Allow-Headers: * Access-Control-Max-Age: 1728000 Server: CherryPy/3.2.0 Date: Thu, 21 Apr 2011 22:37:59 GMT Access-Control-Allow-Origin: * Access-Control-Allow-Methods: POST, GET, OPTIONS Content-Type: text/html;charset=ISO-8859-1 </code></pre> <p><strong>Request Header - POST</strong></p> <pre><code>Host: machineA:8081 User-Agent: ... Firefox/4.01 Accept: */* 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 Keep-Alive: 115 Connection: keep-alive Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Referer: http://jquery.com/ Content-Length: 12 Origin: http://jquery.com Pragma: no-cache Cache-Control: no-cache </code></pre> <p><strong>Response Header - POST (STATUS = 200 OK)</strong></p> <pre><code>Content-Length: 32 Access-Control-Allow-Headers: * Access-Control-Max-Age: 1728000 Server: CherryPy/3.2.0 Date: Thu, 21 Apr 2011 22:37:59 GMT Access-Control-Allow-Origin: * Access-Control-Allow-Methods: POST, GET, OPTIONS Content-Type: application/json </code></pre> <p>I can't work out why the same request would work from one site, but not the other. I am hoping someone might be able to point out what I am missing. Thanks for your help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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