Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable/use cross-origin resource sharing with MVC 3?

I want to submit a form using AJAX to a MVC 3 Controller.

The form and the controller are on two different domains, which is why i want to use CORS.

I have 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 in the controller that takes the form data? As far as i know, there has to be some exchange of data between the client posting data and the server, to determine wether CORS is enabled/supported or not, so i figure that the one line of code has to go somewhere else?

Thanks

like image 220
Kenci Avatar asked May 21 '12 07:05

Kenci


1 Answers

This could go in the controller. Actually I would probably externalize it in a custom action filter to avoid repeating it in every controller action that needs to be invoked from a cross domain AJAX call. There are no additional steps necessary. Just make sure that your browser supports CORS because if it doesn't adding this line will have strictly no effect whatsoever.

like image 111
Darin Dimitrov Avatar answered Nov 15 '22 11:11

Darin Dimitrov