weixin_33693070 2015-05-14 05:26 采纳率: 0%
浏览 41

将ajax调用翻译为CORS

I am trying to access an API via my localhost using the following code:

  $.ajax({
            url: "http://domain.xxx.net/api/[email protected]&password=xxx",
            //method: "GET",
            headers: { "Accept": "application/xml", "Content-Type": "application/xml", "Access-Control-Allow-Origin": "*"},
            dataType: "JSON",
            success:function(data){
                alert(data);},
            success:function(e){
                alert(e.message);}

    });

I have read over the internet that this is not possible due to the error below.

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

After some research it was said that i can use JSONP or CORS. I was able to use JSONP however, I am getting a js error because the API supports only both JSON & XML and not JSONP. Now I am trying to convert my ajax call to CORS but I simply can't understand the syntax. I always get the same error about the 'Access-Control-Allow-Origin'.

I hope someone can help me translate this one. Thanks.

  • 写回答

3条回答 默认 最新

  • weixin_33744854 2015-05-14 05:33
    关注

    Access-Control-Allow-Origin should be in the response header. It sounds the server from which you're trying to get the data did not enable CORS (set a response header of Access-Control-Allow-Origin). If this is the case I don't think you'll be able to get your resource through Javascript. You'll need to do it from the backend.

    评论

报告相同问题?