簡體   English   中英

Cordova(Phonegap)https工作和調試

[英]Cordova (Phonegap) https work and debug

有API的https站點。

需要來自cordova(angularjs)的https API。

我也想在webbrowser(chrome)中調試angularjs應用程序,因為它非常快(與重建和部署相比)。

簡單的代碼

$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. Cordova調試的結果(Visual Studio,Windows-x64)

第一個要求

HTTP500:服務器錯誤-服務器遇到意外情況,阻止其滿足請求。 (XHR):GET- https://.../api/Auth?AspxAutoDetectCookieSupport = 1

兩次,因為$ http + $ .ajax

第二個和下一個請求- 返回正確的響應(也是兩次)

  1. 產生鍍鉻

所有要求

a)$ http使用OPTION方法而不是POST

b)$ http錯誤XMLHttpRequest無法加載https://.../api/Auth 飛行前響應無效(重定向)

c)$ .ajax使用正確的POST

d)$ .ajax錯誤XMLHttpRequest無法加載https://.../api/Auth 所請求的資源上沒有“ Access-Control-Allow-Origin”標頭。 因此,不允許訪問源' http://local1.test '。

問題

  1. 如何在具有相同代碼的cordova和chrome中使用https?

  2. Cordova如何正確跳過第一個請求並作為第二個處理?

從Cordova 5開始,您需要使用Cordova白名單插件

安裝該插件后,您需要修改項目的config.xml文件。 對於您的網絡請求,您需要添加類似於以下內容的<access>標簽:

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

您還需要為您的應用配置內容安全策略 我建議參考Cordova白名單插件文檔以獲取指導。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM