json not working in cross domain

Jak
Jak
Member
352 Points
151 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: 8127
Total Answered: 3
Total Marked As Answer: 1
Posted On: 15-Oct-2014 03:31

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Moderator
1170 Points
210 Posts
     
Posted On: 15-Oct-2014 03:33
Nice One
Nice One
Member
110 Points
10 Posts
Marked as answer      

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
110 Points
10 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
banner

Blog

Active User (0)

No Active User!
 Log In to Chat