how to configure server host ip address and port in cordova app for ajax calls
I am trying to convert one of my web app to hybrid mobile app using cordova. I am using polymer and web components. I am able to render the UI in ios device also. However I want to know how to configure IP address and port of my server (my laptop in this case) dynamicallly when building or depolying the app. May be some cordova setting (like env variable or build argument)
my html
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="styles/app-theme.html">
<link rel="import" href="bower_components/paper-toolbar/paper-toolbar.html">
<link rel="import" href="bower_components/paper-drawer-panel/paper-drawer-panel.html">
some ajax call.
<iron-ajax id="resetValidate" **url="/api/appusers/reset"** method="post" content-type="application/json" on-response="handleResponse" on-error="handleError"></iron-ajax>
....
So actually I want to call http::/192.168.1.39:3000//api/appusers/reset in this case, but changing code is not a good solution, as I have more than 100 places and more over this ip will be different for all developers and also it will change every time. When application will be ready obviously I will need to configure some baseurl for all apis also, so I want to know what is the way in cordova for this.
This works in web application / browser as I render the html and js files as well api from same express server. So hostname and port is same for static and dynamic content and is localhost during development.
Just use a javascript global variable. Then instead of hard-coding the url, just use the global variable concatenated with the rest of your path. That way, you only need to set the url in one place for each developer machine.
YourCustomConfig.js
var globalServerUrl = "http://localhost:3000";
This is the part that changes per dev machine.
AllOtherFilesNeedingToUseTheUrl.js
globalServerUrl + "/the/rest/of/your/path"
everywhere you build the path for an http request. None of these will ever need to be updated again.
Make sure that the value of that global variable is set to your production server location in source control, and you're good to go.
上一篇: 如何在场景中使用INNER JOIN?