AJAX GET JSON似乎什么都不返回

时间:2015-01-01 21:49:42

标签: javascript php android ajax json

我正在使用英特尔XDK创建一个Android应用程序,出于某些原因它似乎在模拟器上运行但在移动设备上似乎失败了。以下是模拟器和设备上的外观截图。

On Emulator, as you can see it says Hello Script47, it has got the session data from my server.

This is the image on the testing device, as you can see it shows nothing, it should say Hello "Session Name Here"

两者都使用完全相同的代码,并指向正确的数据库和服务器。下面是我正在使用的代码,PHP和JavaScript代码。

function getSessionData(callback) {
    $.ajax({
        url: 'http://sm.script47.net/api/v1/modules/sessionData/sessionData.php',
        "type": "GET",
        dataType: "json",
        async: true,
        success: function (data) {
            callback(data);
        },
        error: function (xhr, textStatus, error) {
            alert(xhr.statusText);
            alert(textStatus);
            alert(error);
        },
    });
}

然后在另一个名为home.js。

的文件中
window.onload = function () {
    getSessionData(function (data) {
        window.token = data.token;
        window.userID = data.userID;
        window.username = data.username;
        window.emailAddress = data.emailAddress;

        alert(window.userID);

        getElement("userDetails").innerHTML = "Hello <strong> " + window.username + "</strong>"
    });
};

然后获取会话数据我在namecheap(共享主机)托管的PHP服务器上使用下面的内容。

<?php
session_start();

// I added the code below in the comment block to try and make it work.
/******************************************************/
header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");
/******************************************************/

$sessionData = array(
    "token" => htmlspecialchars(trim($_SESSION['token'])),
    "userID" => htmlspecialchars(trim($_SESSION['userID'])),
    "username" => htmlspecialchars(trim($_SESSION['username'])),
    "emailAddress" => htmlspecialchars(trim($_SESSION['emailAddress']))
);
echo json_encode($sessionData, JSON_PRETTY_PRINT);

我不知道为什么它不起作用或是什么导致它不起作用,这就是为什么我不确定修复是什么。我已经谈到了namecheap客户支持,他们说AJAX请求应该可以工作,并且他们使用它们来登录/注册,但是在实际的移动设备上获取JSON会话数据似乎不起作用。成功/错误回调也不会返回任何内容。

0 个答案:

没有答案