How to enable / use resource sharing using MVC 3?

I want to submit a form using AJAX to an MVC 3 controller.

The form and controller are in two different domains, so I want to use CORS.

I read that the following code should do the trick in ASP.NET:

Response.AppendHeader("Access-Control-Allow-Origin", "*"); 

from http://enable-cors.org/#how-asp.net

Should this code go directly to the controller that accepts the form data? As far as I know, there needs to be some kind of data exchange between the client and server hosting data to determine if CORS / is supported or not, so I think that one line of code should go somewhere else?

thanks

+7
source share
1 answer

This can go to the controller. In fact, I would most likely implement it in a custom action filter to avoid repeating it in every controller action that needs to be called from an AJAX cross-domain call. No additional steps required. Just make sure your browser supports CORS, because if it does not add this line, it will be completely inefficient.

+3
source

All Articles