CORS - Cross-Domain AJAX Without JSONP By Allowing Origin On Server

后端 未结 3 1894
无人共我
无人共我 2020-11-30 13:13

I have two separate apps on the same server, with the EmberJS one trying to do cross-domain calls to my backend API.

I set up my backend API to allow cross-domain re

3条回答
  •  我在风中等你
    2020-11-30 13:40

    There is no need to use JSONP if you enable CORS.

    Access-Control-Allow-Origin: http://www.example.com
    

    if this header is set in the response, then normal XmlHttpRequest will be able to access the response as if it is like same domain. Check whether this header is set correctly.

    I hope that this link will help you if you are using jquery A CORS POST request works from plain javascript, but why not with jQuery?

    Update: Example

    var xmlhttp= new XMLHttpRequest();
    var url="https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control";
    xmlhttp.open("GET",url,false);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlhttp.send();
    

    Try this in any domain, you will get response.

    Update solution:

    Request url without "http://" caused the problem, prepending "http://" solved the issue

提交回复
热议问题