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

后端 未结 18 3544
北海茫月
北海茫月 2020-11-21 05:52

I\'m making an Ajax.request to a remote PHP server in a Sencha Touch 2 application (wrapped in PhoneGap).

The response from the server is the following:

18条回答
  •  没有蜡笔的小新
    2020-11-21 06:03

    This was the first question/answer that popped up for me when trying to solve the same problem using ASP.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 ASP.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.

    For example:

    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" );
    }
    

提交回复
热议问题