Elice Ee Elice Ee - 12 days ago 8
jQuery Question

Jquery-No 'Access-Control-Allow-Origin' header is present on the requested resource

I have request json data from other website, but it meets the problem of access control allow origin in header, I have no idea how to set the access control allow origin in header, I'm put my source code in the iis8 to access the json data from another iis8 api source.

$.ajax({
type: "GET",
url:rootURL,
xhrFields: {
withCredentials: false
},

headers: {
"Access-Control-Allow-Origin: ": "*",
"Access-Control-Allow-Methods: ": "GET",
"Access-Control-Allow-Headers: ": "Authorization",
},
dataType: "json",
success: function(data) {
},
error: function() {
alert("An error occurred while processing JSON file.");
}
});

Answer

These should be in the server, not the client:

"Access-Control-Allow-Origin: ": "*",

To implement that in server, for PHP:

<?php
  header("Access-Control-Allow-Origin: *");

For ASP.NET:

Response.AppendHeader("Access-Control-Allow-Origin", "*");

For others, check I want to add CORS support to my server for more information.