CORS error with jquery

后端 未结 4 1152
后悔当初
后悔当初 2020-12-01 21:58

I\'m currently working on a project using the cloudapp API and I\'m using jquery. Here is my code:

 $.ajax({
            headers: { \"Accept\": \"application         


        
4条回答
  •  旧时难觅i
    2020-12-01 22:49

    CORS (Cross-Origin Resource Sharing) is an HTML5 feature that allows one site to access another site’s resources despite being under different domain names.

    The W3C specification for CORS actually does a pretty good job of providing some simple examples of the response headers, such as the key header, Access-Control-Allow-Origin, and other headers that you must use to enable CORS on your web server.

    If you are looking for specific information on how set up CORS on a variety of common web servers, check out the Enable CORS sharing website. http://enable-cors.org/

    Based on the URL that you have given above, I don't think you have a control on that server. Thus, it will simply would not work.

    Even though you have enabled crossDomain: true the server should return you required headers such as:

    Here’s a valid server response; the CORS-specific headers are bolded

    HTTP Response:

    Access-Control-Allow-Origin: http://xyzcom
    Access-Control-Allow-Credentials: true
    Access-Control-Expose-Headers: FooBar
    Content-Type: text/html; charset=utf-8
    

    One thing you can try is this:

    $.ajax({
                headers: { "Accept": "application/json"},
                type: 'GET',
                url: 'http://cl.ly/2wr4',
                crossDomain: true,
                beforeSend: function(xhr){
                    xhr.withCredentials = true;
              },
                success: function(data, textStatus, request){
                    console.log(data);
                }
     });
    

    Otherwise, you need to contact API provider.

提交回复
热议问题