I have a application hosted with domain www.abc.com .This is actually WebApi application . I want to build a separate UI application using asp.net mvc, and its domain is www.xyz.com. The UI application consuming Angular,HTML5 and other Web technologies.Here i really need to perform GET,PUT,POST,DELETE against www.abc.com. unfortunately the limitation of jsonp , i like to choose CORS (HTML5 Cors), but i can't pass json object to CORS calls . what are the best possible best approach to call cross domain calls (GET,PUT,POST,DELETE) using angularJs with my problem scenario . What should i change in IIS to handle CORS request.
2 Answers
add below web.config in webapi
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
and in angular, set below config options for $http
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: http://url.com:8080");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
also check: this link
1 Comment
T_Dnzt
Quick note to say that the domain name was changed. If you want to check the link mentioned above, see samurails.com/tutorial/cors-with-angular-js-and-sinatra
To enable cross domain ajax calls you can place crossdomain.config in asp.net web api root folder when published with following contents.
<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*"/>
</cross-domain-policy>
2 Comments
Ray Nicholus
The OP asked about CORS, not JSONP. Your solution will only work for GET requests anyway.
aamir sajjad
@Ray Nicholus I update my answer. on other hand, I implemented a web services using asp.net web api for mobile client for which I need to manage/enable cross domain ajax calls. So I place the crossdomain.config with contents above in application root folder while publishing, and it enables us to access the web api end points. Therefore, I don't understand why you down rated it. Also, it should work fine for both get and post. You are ought to explain with reference where I am wrong with my answer. So that I can correct myself.