I create a simple MVC Controller action that takes some json data and then returns true or false.
[AllowCrossSiteJson] public JsonResult AddPerson(Person person) {
I call it from javascript:
function saveData(person) { var json = $.toJSON(person); //converts person object to json $.ajax({ url: "http://somedomain.com/Ajax/AddPerson", type: 'POST', dataType: 'json', data: json, contentType: 'application/json; charset=utf-8', success: function (data) { alert("ok"); } }); }
Everything works as long as I am in one domain, but as soon as I call it from another domain, I have problems.
There is an “AllowCrossSiteJson” action filter on the controller, which sets the “Access-Control-Allow-Origin” header to “*”, allowing any source to access the controller action.
public class AllowCrossSiteJsonAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*"); base.OnActionExecuting(filterContext); } }
However - I get this error in firebug when calling through domains:
OPTIONS http://somedomain.com/Ajax/AddPerson?packageId=3 500 (Internal server error) XMLHttpRequest cannot load http://somedomain.com/Ajax/AddPerson . The Content-Type request header field is not allowed in the Access-Control-Allow-Headers headers.
What is wrong here?
I have been looking through possible solutions for several hours, and this is similar to jquery using OPTIONS (not POST, as I would expect).
If this is really a problem, how can I fix it?