Resolve: Origin is not allowed by Access-Control-Allow-Origin

This was the first q/a that popped up for me when trying to solve the same problem using .net MVC as the source of my data. I realize this doesn’t solve the php question but it is related enough to be valuable.

I am using .net mvc. The blog post from Greg Brant worked for me. Ultimately you create an attribute :

[HttpHeaderAttribute("Access-Control-Allow-Origin", "*")]

That you are able to add to controller actions.

eg:

public class HttpHeaderAttribute : ActionFilterAttribute
{
   public string Name { get; set; }
   public string Value { get; set; }
   public HttpHeaderAttribute(string name, string value)
   {
      Name = name;
      Value = value;
   }

   public override void OnResultExecuted(ResultExecutedContext filterContext)
   {
      filterContext.HttpContext.Response.AppendHeader(Name, Value);
      base.OnResultExecuted(filterContext);
   }
}

and then using it with:

[HttpHeaderAttribute("Access-Control-Allow-Origin", "*")]
public ActionResult MyVeryAvailableAction(string id)
{
    return Json( "Some public result" );
}

Source: http://goo.gl/WBsnr

Other view of the problem in…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s