0

I am building a SPA ember.js app that will hit a .net API via an ajax call.

ember.js

getData: function(){
  $.ajax({
    type: "POST",
    url: "http://localhost:9001/controller",
    dataType : "json",
    headers: "Access-Control-Allow-Origin",
    contentType: 'application/json; charset=utf-8',
    success : function(data) {
        return data;
    },
    error:function(data){
      alert('test' + data);
    }
  })
}

It returns an error message : SyntaxError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'function () { return _emberRuntimeSystemString.fmt(this, arguments); }' is not a valid HTTP header field value.

I have been hammering away at this issue for a few hours now and I just can't seem to get around it. Also very new to ember.js.

If anyone has a better idea of whats going on...

jaredlee.exe
  • 81
  • 1
  • 9
  • 1
    `url: "http://localhost:9001/controller"` modify this to `url: "controller/method"` and that should work – manish Dec 03 '15 at 12:29
  • I tried that as well as removing the header. The response was: "NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost:9001/controller/method'." – jaredlee.exe Dec 03 '15 at 12:51
  • 1
    you got me wrong `controller/method` means `your-controller-name/` followed by the method name which'll get you actual data `method-name` – manish Dec 03 '15 at 13:09
  • I was using an incorrect URL and the header value was incorrectly used in my ajax call – jaredlee.exe Dec 03 '15 at 13:20

1 Answers1

1

Well, the problem is in your headers. You are not giving "Access-Control-Allow-Origin" a value. So you want something like

headers: {"Access-Control-Allow-Origin": 'value' }

Also, I don't think that particular header is used that way? I believe it's a header that the server sends. Check this post out - How does Access-Control-Allow-Origin header work?

Pav

Community
  • 1
  • 1
Pavdro
  • 411
  • 1
  • 9
  • 18
  • I tried your answer and the response message is now just "error". The console is logging : 'XMLHttpRequest cannot load http://localhost:9001/controller. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 404.' – jaredlee.exe Dec 03 '15 at 12:20
  • Yep, your requested server must return 'Access-Control-Allow-Origin' header. You need to return 'Access-Control-Allow-Origin: http://localhost:9001' header from your controller service – Pavdro Dec 03 '15 at 12:28
  • I removed the header. The response was: "NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'localhost:9001/controller/method'." So I think this is more of an issue with the controller – jaredlee.exe Dec 03 '15 at 12:52
  • I was using an incorrect URL and the header value was incorrectly used in my ajax call – jaredlee.exe Dec 03 '15 at 13:23