I have a C # web API project. I created a controller using the Web API 2 standard. I try to include CORS in only one action, this is what I have:
namespace MyProject.Controllers
{
public class MyControllerController : ApiController
{
[EnableCors("http://link.myurl.com", "*", "*")]
public bool Post(MyCustomObject customObject)
{
return myFunction(customObject);
}
}
}
My WebApiConfig.cs looks like this:
config.EnableCors();
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore };
Here is an AJAX cross-domain call:
$.ajax({
type: "POST",
url: 'http://myurl.com/api/mycontroller',
data: JSON.stringify({ Type: 'TextHere', OtherInfo: 'TextHere' }),
contentType: "application/json",
success: function (data) {
}
});
Based on tutorials like this one: http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api#how-it-works , I have to get everything right tune.
I keep getting the following error in Chrome:
XMLHttpRequest http://myurl.com/api/mycontroller. Access-Control-Allow-Origin . Origin 'http://link.myurl.com' .
Fiddler, , Origin 200, , 500.
, , . , Origin , Allow Origin.
web.config:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://link.myurl.com" />
</customHeaders>
</httpProtocol>
<system.webServer>
[EnableCors], , (http://link.myurl.com), . [EnabledCors], .
, . , ( ), JSON.
HELP?