0

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.

xpg94
  • 495
  • 1
  • 10
  • 26
  • Your comment indicates that you believe Ember is buggy? Are you familiar with how cross-domain requests work? (Assuming your client code and the `host` property are different hosts) – Steve H. Apr 08 '16 at 18:53
  • 1
    You may want to learn about this topic, (BTW, it has nothing to do with Ember, it's the way browsers work): https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS – Steve H. Apr 08 '16 at 19:13
  • 1
    I was too stressed out yesterday because of this issue so I thought the ember was buggy. I was not familiar with CORS at all. Now I will try to allow CORS on back end to fix the problem. Thanks :) – xpg94 Apr 09 '16 at 08:00

1 Answers1

1

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

ykaragol
  • 6,139
  • 3
  • 29
  • 56
  • Or possibly the server does not support the OPTIONS request. Has anyone got any knowledge about turning off the preflight request in ember data? The WP-REST-API seems not to support it (unbelievably) – Epirocks May 04 '16 at 00:08
  • You cannot turn it off. This is done by browser. – ykaragol May 04 '16 at 03:50
  • Actually you can stop the browser doing it by setting the content type to text/plain in your Ember data adapter. It's only for development so I don't see the point in supporting OPTIONS. – Epirocks May 13 '16 at 18:53