PhoneGap developing cross-platform mobile apps-addressing cross-domain resource sharing

Source: Internet
Author: User


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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.