I hosted my client on localhost:8080/ and server on localhost:44302/
I am trying to link to my backend, but I am getting CORS issue. Below is my Angular http request
$http.post(url, data, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS'
}
}).success(function (response) {
// rest of the code
}).error(function (err, status) {
// rest of the code
});
On the server side which is written in C#, I have the following set on the response
Response.ContentType = "application/json";
Response.AddHeader("Access-Control-Allow-Origin", "*");
Response.AddHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
Even after setting these, I am getting the following error
XMLHttpRequest cannot load https://localhost:44302/some_uri. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 400.
What am I missing?
I hosted my client on localhost:8080/ and server on localhost:44302/
I am trying to link to my backend, but I am getting CORS issue. Below is my Angular http request
$http.post(url, data, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS'
}
}).success(function (response) {
// rest of the code
}).error(function (err, status) {
// rest of the code
});
On the server side which is written in C#, I have the following set on the response
Response.ContentType = "application/json";
Response.AddHeader("Access-Control-Allow-Origin", "*");
Response.AddHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
Even after setting these, I am getting the following error
XMLHttpRequest cannot load https://localhost:44302/some_uri. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 400.
What am I missing?
Share Improve this question asked Jun 8, 2015 at 22:43 Abhijith NagarajaAbhijith Nagaraja 3,3806 gold badges30 silver badges57 bronze badges 2- 1 What are you using for backend? ASP.NET Web API? Where did you put the code that modifies the response? – SoftwareFactor Commented Jun 8, 2015 at 22:49
- Yes am using ASP.NET MVC, I put the code in the controller from where I am sending the response back – Abhijith Nagaraja Commented Jun 8, 2015 at 22:51
2 Answers
Reset to default 3You do not need to send any additional headers from AngularJS. Preflight request will be made for you automatically.
To enable all cross-origin requests in Web API, you can do this:
- Install NuGet package Microsoft.AspNet.WebApi.Cors.
Add the following code to WebApiConfig.cs:
var corsAttr = new EnableCorsAttribute("*", "*", "*"); config.EnableCors(corsAttr);
Full understanding of CORS is worth a few minutes of time. I remend reading an in-depth tutorial here:
http://www.asp/web-api/overview/security/enabling-cross-origin-requests-in-web-api
Perform this in your application level server side No changes required in your client-side application
Step 1: Install-Package Microsoft.AspNet.WebApi.Cors
Step 2: From HttpConfiguration object you can enable cors - Applicable for the entire application
public static void Register(HttpConfiguration config)
{
// New code
config.EnableCors();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Alternate :
Step 3: Go to controller add this below attribute - Applicable only to TestController
// Allow CORS for all origins. (Caution!)
[EnableCors(origins: "*", headers: "*", methods: "*")]
Example :
[EnableCors(origins: "http://mywebclient.azurewebsites", headers: "*", methods: "*")]
public class TestController : ApiController
{
// Controller methods not shown...
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745249059a4618563.html
评论列表(0条)