2

This is pretty much a known standard error but unable to fix the same using existing Stackoverflow posts.

XMLHttpRequest cannot load https://myserver.com/context/
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'https://www.otherwebsite.com' is therefore not allowed access.
The response had HTTP status code 405.

Following is the code I have -

function setHeader(xhr) { xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); } var url = "https://myserver.com/context/"; $.ajax({ url: url, type: 'GET', dataType:'json', success: function(data) { _this.question(data.question); _this.questionId(data.questionId); _this.choices(data.choices); }, error: function() { console.log("ERROR in getting microquestion"); }, beforeSend: setHeader }); 
2
  • 1
    Reading the error message: "No 'Access-Control-Allow-Origin' header is present on the requested resource." Commented Sep 11, 2017 at 5:35
  • I just realized what error was trying to point out. The issue was on the server side. that's what "requested resource" meant. Thanks to @sideshowbarker who explicitly pointed that out. Upvoting both of you Commented Sep 11, 2017 at 7:04

1 Answer 1

1

The “Response to preflight request… had HTTP status code 405.” message indicates, to start, the https://myserver.com/context/ endpoint needs to be configured to handle OPTIONS requests correctly—even if it may already be set to send the right CORS headers back for “normal” requests.

For more details about what’s happening, you can read the How to avoid the CORS preflight section of the answer at No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API — but the gist of it is, minimally, the server must respond to OPTIONS requests with a 2xx success status code.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.