weixin_33749131 2017-01-23 13:18 采纳率: 0%
浏览 41

jQuery ajax的网址无效

My ajax request looks like this:

$.ajax({
    url: self.opts.url.replace('//www.', '//'),
    type: 'POST',
    ...
});

I put that .replace just to be sure. Because my URL in opts.url is "http://website.com/admin/upload-image/123". But I can't upload an image, because I get an error in the console: XMLHttpRequest cannot load http://www.website.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://website.com' is therefore not allowed access.

I double-checked the URL in debug mode and it is always without a www., but jQuery seems like adding it anyway. This doesn't make sense for me at all.

Also what I get from jQuery's error function:

jqXHR: Object (filled statusText with "error")
textStatus: "error"
errorThrown: ""
  • 写回答

1条回答 默认 最新

  • weixin_33725126 2017-01-23 13:24
    关注

    As you can see in the error message, it says:

    XMLHttpRequest cannot load www.website.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'www.website.com' is therefore not allowed access.

    You must set the Access-Control-Allow-Origin Header on the www.website.com webpage. In php this is done by this:

    header("Access-Control-Allow-Origin: http://yourwebsiteThatNeedsAccess");

    评论

报告相同问题?