新页面加载后检索JSON对象

时间:2018-01-29 18:31:59

标签: javascript php json

我不熟悉Web开发环境中的JSON处理,所以希望得到一些指导。

我使用网络表单登录 - 如果成功登录,则返回JSON数组,如下所示:

{
    "result": "success",
    "message": "Login Successful",
    "user": {
        "name": "Foo Bar",
        "email": "foo@bar.com",
        "unique_id": "59bea8b7d56a63.8888888"
    }
} 

我的数据库操作将JSON返回给我的Functions.PHP。然后我做以下事情:

$response["result"] = "success";
$response["message"] = "Login Successful";
$response["user"] = $result;
$json = json_encode($response);

在这里我被困了,因为我想将编码的JSON发送到将成功打开的网页,因为我想根据用户unique_id进行进一步的数据库查询。

我在第1页尝试过:

$response["result"] = "success";
$response["message"] = "Login Successful";
$response["user"] = $result;

$json = json_encode($response);


header('Location: http://example.co.uk/quiz/dashboard.php/'.$json);

第2页

<?php
$data_get = $_REQUEST['user'];
?>
<script type="text/javascript">
var mydata =<?php echo $data_get; ?>;
</script>

但我无法回应用户数据。我希望能够在第2页上检索已编码的数组,然后将其解码并将名称/ email / unique_id存储在变量中,以便在第2页上需要时使用。

3 个答案:

答案 0 :(得分:1)

在您的第1页

$response["result"] = "success";
$response["message"] = "Login Successful";
$response["user"] = $result;

$json = json_encode($response);


header('Location: http://example.co.uk/quiz/dashboard.php/?data='.$json);

在第2页中,您将获得类似的值

$data = json_decode($_GET['data']);

echo $data->message. "</br>";
echo $data->result."</br>";
echo $data->user."</br>";

或者如果你想将json分配给javascript变量,那么

<script type="text/javascript">
var mydata =<?php echo $_GET['data']; ?>;
</script>

答案 1 :(得分:1)

你排队:

$response = json_decode($_GET['data']);

接缝成为问题。

如果你真的想通过GET请求传输整个json字符串,你需要为它定义一个名称。试试这个:

 if(System.nanoTime() - startTime >= 1000000000) {
        Log.d("FPSCounter", "fps: " + frames);
        startTime = System.nanoTime();
        **frames = 0;**
}

然后在第2页,这就是你收到它的方式:

Dim regOffice As Variant                  'Range which will contain all the regional offices
Dim hearingLoc As Variant                'Range which will contain the hearingLoc specific to the Reg Office


        regOffice = Evaluate("TEST") 'The purpose here is to use regOffice range later and iterate through it's values.

        'Office Location Validation
        With Range("B24", "B100").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Formula1:=Evaluate("TEST")
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = False
        End With

        'Member Name Validation
        With Range("D24", "D100").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Formula1:=Evaluate("TESTMEMBERS")
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = False
        End With

之后,您可以访问与第1页相同的$ response数组。

顺便说一下:将数据存储在session

可能更容易

答案 2 :(得分:0)

用你正在做的事情在文件2中尝试这个:

$decoded=json_decode(array_keys($_REQUEST)[0]);
echo $decoded->user;
相关问题