While developing a Backbone.js based application, I encountered an unusual issue.
When the app requests a collection resource at a specific point, I receive an error in Chrome and Safari that reads:
XMLHttpRequest cannot load http://api.mydomain.net/v1/foos/00000d/bars/000014/boots Origin http://localhost:3501 is not allowed by Access-Control-Allow-Origin.
Initially, I assumed it was a CORS problem and blamed my API. However, when I tried to request the same resource using CURL:
curl -i -H'Accept: application/json' -H'X-Auth-Token: pAWp5hrCmXA83GgFzgHC' -XOPTIONS 'http://api.mydomain.net/v1/foos/00000d/bars/000014/boots'
HTTP/1.1 200 OK
Status: 200
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-Auth-Token
Content-Length: 0
Seems fine so far, but when I tried the GET request:
curl -i -H'Accept: application/json' -H'X-Auth-Token: pAWp5hrCmXA83GgFzgHC' -XGET 'http://api.mydomain.net/v1/foos/00000d/bars/000014/boots'
HTTP/1.1 204 No Content
Status: 204
Cache-Control: no-cache
Content-Length: 0
Content-Type: text/plain
It only works if the boots collection contains at least one object. The CORS headers from my server seem correct. So why do the browsers still flag it as a cross-origin resource problem?
Could it be because of the content type text/plain
in my 204 responses?
Preflight (OPTIONS) request in dev tools: https://i.sstatic.net/Viifh.png
Request headers of aborted response: https://i.sstatic.net/XdOgs.png