Search code examples
javascriptember.jsember-datajson-apiget-request

emberJS JSONAPI adapter sends OPTION instead of GET request


I am trying to make basic request to a working server (checked through google POSTMAN) in an emberJS application. But when I try it, I get http 404 error. Then I inspected the error, and saw that the original request which comes from client side came as OPTION request instead of GET request. Also, I've set two header, Accept and Content-Type to be application/json and when inspecting, inspector printed these headers:

Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:GET
Connection:keep-alive
Host:frontend.com
Origin:http://localhost:4200
Referer:http://localhost:4200/restaurant
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36

Now here is the adapter

//Application.js adapter:
import DS from 'ember-data';

export default DS.JSONAPIadapter.extend({
    host: 'http://server.com',
    namespace: 'v1',
    headers: {
      "Accept": "application/json",
      "Content-Type": "application/json"
    }
});

Is there some way to turn this OPTION off, or is there some workaround? I didn't believe ember was so buggy, because these is supposed to be one of main things that emberJS should do - send requests. Any help is appreaciated.


Solution

  • This is "request preflight". While doing a cross origin request, browser first sends a preflight request by sending an OPTION request. If the OPTION request is responded successfully, the real request would be send to the server.

    The "preflight" fails in such cases:

    • CORS is not enabled by the server
    • authentication / authorization problems are occured
    • service function is not found (404)
    • service function do not accept the header you will send

    You may search with these keywords: preflight, cors