weixin_33697898 2016-12-19 07:10 采纳率: 0%
浏览 27

通过CORS政策[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/35553500/xmlhttprequest-cannot-load-xxx-no-access-control-allow-origin-header" dir="ltr">XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header</a>
                            <span class="question-originals-answer-count">
                                (8 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2016-12-19 07:14:33Z" class="relativetime">3 years ago</span>.</div>
        </div>
    </aside>

I have tried all I could after reading CORS policy from MDN , and even using the below code from from https://www.html5rocks.com/en/tutorials/cors/. I just wanted to fetch a wiki page(given). It spit error message which shows that it runs onerror method. At the console I prints "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://en.wikipedia.org/wiki/Main_Page. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)."


// Create the XHR object.
function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    // XHR for Chrome/Firefox/Opera/Safari.
    xhr.open(method, url, true);
  } else if (typeof XDomainRequest != "undefined") {
    // XDomainRequest for IE.
    xhr = new XDomainRequest();
    xhr.open(method, url);
  } else {
    // CORS not supported.
    xhr = null;
  }
  return xhr;
}

// Helper method to parse the title tag from the response.
function getTitle(text) {
  return text.match('(.*)?')[1];
}

// Make the actual CORS request.
function makeCorsRequest() {
  // This is a sample server that supports CORS.
 // var url = 'http://html5rocks-cors.s3-website-us-east-1.amazonaws.com/index.html';
 var url = 'https://en.wikipedia.org/wiki/Main_Page';
  var xhr = createCORSRequest('GET', url);
  if (!xhr) {
    alert('CORS not supported');
    return;
  }

  // Response handlers.
  xhr.onload = function() {
    var text = xhr.responseText;
    var title = getTitle(text);
    alert('Response from CORS request to ' + url + ': ' + title);
  };

  xhr.onerror = function() {
    alert('Woops, there was an error making the request.');
  };

  xhr.send();
}

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33691817 2016-12-19 07:25
    关注

    You can use iframe,

    document.getElementById("iFrame").src ="https://en.wikipedia.org/wiki/Main_Page";
    评论

报告相同问题?