简体   繁体   中英

Cordova (Phonegap) https work and debug

There https site with api.

Need work from cordova (angularjs) with it`s https api.

Also I want debug angularjs app in webbrowser (chrome), because its very quick (compared with rebuild and deploy).

Simple code

$http({
    method: 'POST',
    crossDomain: true,
    xhrFields: { withCredentials: false },
    url: 'https://.../api/Auth',
    data: { email: user, password: password }
    }).
    then(function (response) {
        console.log("0", response);
    }, function (response) {
        console.log("1", response);
    });

$.ajax({
    type: "POST",
    url: 'https://.../api/Auth',
    xhrFields: {
        withCredentials: true
    },
    crossDomain: true,
    data: { email: user, password: password },
    success: function (response) { console.log("2", response) },
});
  1. Result in cordova debug (Visual studio, Windows-x64)

First request

HTTP500: SERVER ERROR - The server encountered an unexpected condition that prevented it from fulfilling the request. (XHR): GET - https://.../api/Auth?AspxAutoDetectCookieSupport=1

twice, because $http + $.ajax

Second and next request - Return correct response (twice too)

  1. Result in chrome

All requests

a) $http use OPTION method not POST

b) $http error XMLHttpRequest cannot load https://.../api/Auth . Response for preflight is invalid (redirect)

c) $.ajax use correct POST

d) $.ajax error XMLHttpRequest cannot load https://.../api/Auth . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://local1.test ' is therefore not allowed access.

Questions

  1. How work with https in cordova and chrome with the same code?

  2. How correct in cordova skip first request and work with as second?

As of Cordova 5, you'll need to use the Cordova Whitelist Plugin .

Once that plugin is installed, you need to modify your project's config.xml file. For your network requests, you'll need to add <access> tags similar to:

<access origin="http://*.google.com" />

You'll also need to configure the Content Security Policy for your app. I recommend referring to the Cordova Whitelist Plugin documentation for guidance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM