【问题标题】:phonegap android ajax over ssl failesphonegap android ajax over ssl 失败
【发布时间】:2016-10-19 12:12:37
【问题描述】:

我使用 Phonegap 和 Jquery Mobile 开发了一个应用程序。由于某些原因,我还需要从 https 域获取数据。在 IOS 上一切正常,但在 Android 上我总是收到以下错误并且请求失败

06-17 17:16:33.890 6079-6154/de.sistecs.einlass E/chromium_net: external/chromium/net/socket/ssl_client_socket_openssl.cc:905: 
[0617/171633:ERROR:ssl_client_socket_openssl.cc(905)] handshake failed; returned -1, SSL error code 1, net_error -107

这是一个简单的示例代码

$(document).on("pageinit", function (event, ui) {
    $("#test").click(function () {
        $.ajax({
            type: "POST",
            url: "https://test.sistecs.de/sismedia/test.php",
            success: function (response) {
                $("#testmsg").text(response);
            }
        });
    });
});

我已经阅读了数百篇帖子,但没有找到解决问题的方法。 服务器https://test.sistecs.de/sismedia/test.php的证书有效。

有人可以帮我吗?

【问题讨论】:

    标签: android jquery cordova ssl jquery-mobile


    【解决方案1】:

    这似乎是一个跨域问题。

    使用 jQuery Mobile,您应该将 $.mobile.allowCrossDomainPages 和 $.support.cors 设置为 true

    <script>
        $( document ).on( "mobileinit", function() {
            $.mobile.allowCrossDomainPages = true;
            $.support.cors = true;
        });
    </script>
    

    还要确保您的 PHP 页面相应地设置了标题。例如:

    <?php
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET,PUT,POST,DELETE');
        header('Access-Control-Allow-Headers: Content-Type');
        echo "test";
    ?>
    

    编辑:我没有测试代码,这是一个例子。根据需要进行调整。

    【讨论】:

      【解决方案2】:
         var myJson = {
              'Par1': 'par'
          };
      
          $.ajax({
              url: baseUrlAjaxServices + "/....",
              data: JSON.stringify(myJson),
      
              method: "POST",
              crossDomain: true,
              dataType: 'json',
              contentType: 'application/json',
          })
        .done(function (data) {
           //...
        }).fail(function (error) {
            console.log(error);
        });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-28
        • 1970-01-01
        • 1970-01-01
        • 2020-11-20
        • 1970-01-01
        • 2014-08-12
        相关资源
        最近更新 更多