json not working in cross domain

Jak
Jak
Member
858 Points
132 Posts

I have following error message:

No Access-Control-Allow-Origin header is present on the requested resource. Origin https://webcache.googleusercontent.com is therefore not allowed access.

Views: 9816
Total Answered: 3
Total Marked As Answer: 1
Posted On: 15-Oct-2014 03:31

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Teacher
4822 Points
23 Posts
         
Posted On: 15-Oct-2014 03:33
Nice One
Nice One
Member
280 Points
0 Posts
         

Hi,

Create a new attribute

public class AllowCrossSiteJsonAttribute:ActionFilterAttribute
{
public overridevoid OnActionExecuting(ActionExecutingContext filterContext)
{ filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin","*");
base.OnActionExecuting(filterContext);
}
}

Tag your action:

[AllowCrossSiteJson]
public ActionResult YourMethod()
{
return Json("Works better?");
}
Posted On: 20-Oct-2014 00:37
Nice One
Nice One
Member
280 Points
0 Posts
         

Hi,

You can do this from web.config as (in <system.webServer>...</system.webServer>):

<httpProtocol>
<customHeaders>
<!--Enable Cross Domain AJAX calls -->
<removename="Access-Control-Allow-Origin" />
<addname="Access-Control-Allow-Origin"value="*" />
</customHeaders>
</httpProtocol>
Posted On: 26-Nov-2014 07:55
 Log In to Chat