Resolving cross-domain resource sharing
First, Webapi solve cross-domain resource sharing.
In the development of selecting WEBAPI as the data interface of the server, because of the use of phonegap, it is necessary to from remote remote data servers via JS, which involves cross-domain resource sharing due to the restriction of homologous policies.
Start by creating a simple Webapi project,
Use Microsoft's Cors solution, and then download microsoft.aspnet.webapi.corsin Nugget.
Configuration webapiconfig:
Here first Test with *, in the production environment, so configuration is not safe, need to make some restrictions.
Config. Enablecors (NewEnablecorsattribute ("*","*","*"));//Web API Configuration and Services//Configure Web API to use only bearer token authentication.CONFIG. Suppressdefaulthostauthentication (); config. Filters.add (NewHostauthenticationfilter (Oauthdefaults.authenticationtype));//Web API RoutesCONFIG. Maphttpattributeroutes (); config. Routes.maphttproute (Name:"Defaultapi", Routetemplate:"Api/{controller}/{id}", defaults:New{id =routeparameter.optional});
To create a apicontroller, the default is:
Well, the server, even if it's simply configured,
Re-open a project and write a simple Ajax visit:
<script type="Text/javascript">$ (function () {$.ajax ({type:"Put", URL:"HTTP://LOCALHOST:10734/API/CHARGING/2",//The http://localhost:10734 here is the service address of the WEBAPI operation data: {"":"Post Method 2"}, Success:function (data) {console.debug (data); $("#divcontent"). text (data); } }); }) </script>
Well, test it and find that you can get the return data.
This is an extremely simple homologous strategy solution.
There are more, more detailed solutions, later slowly updated ...
PhoneGap developing cross-platform mobile apps-addressing cross-domain resource sharing