donglan8256 2015-09-15 14:07
浏览 570

简单的HTTP GET无法使用https来获取JSON

I have this working PhoneGap application built with AngularJS and Onsen UI, that accesses a PHP file where it gets some data in the response. The php file was on a domain with simple http, but now that I moved it to one that supports https , I keep bumping into some weird problems.

I tried the AngularJS async way of getting data, and also an ajax call.

$http.get('https://blablabla.com/file.php?parameters').then(function(response) {
        //SUCCESS
        alert(response);
        var z = response;
    },function(response) {
        alert("Failed");
});

It goes to the SUCCESS part, but the data that I have in it is incorrect. It should be a string (a plain JSON string, that I will stringify and convert to a json object)

The ajax call is the following:

function httpGet(theUrl) {
    var dataToReturn;
    $.ajax({
        type: 'GET',
        url: theUrl,
        cache: false,
        async: false,
        data: "",
        success: function(data, textStatus, result) {
            dataToReturn = data;
        },
        error: function (result, textStatus, errorThrown) {
            alert("Failed to get response from server!");
        }
    });

    return dataToReturn;
    //var xmlHttp = new XMLHttpRequest();
    //xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
    //xmlHttp.send( null );
    //return xmlHttp.responseText;
};

I'm basically calling a php script that is in https, from a PhoneGap app. If I change the address to http, it works flawlessly...

I also want you to note, that PhoneGap basically creates some sort of local proxy. I'm not really sure how to elaborate on this, though...

Any ideas on how to solve this, to get the same data as I would when the addresses are simple "http"?

  • 写回答

1条回答 默认 最新

  • douzhi9921 2015-09-15 14:17
    关注

    Angular automatically converts json, better if you set the proper header from your PHP serverside:

    header('Content-Type: application/json');
    

    just before the echo.

    Second your get seems to be missing a parenthesis

    $http.get('https://blablabla.com/file.php?parameters')
        .then(function(response) {
            //SUCCESS
            alert(response);
            var z = response;
        },function(response) {
            alert("Failed");
        });
    

    Third, the response doesn't go straight on response anymore but response.data so you might want to try

    alert(response.data);
    var z = response.data;
    

    Warning! this varies a lot depending on your Angular.js version

    there are several different syntaxes to use for the $http object depending on the angular version you're on.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题