我仍在努力将JSON数据传递到另一个页面.有什么想法吗


I am still struggling to pass JSON data to another page. Any ideas how this is done?

请原谅我发了太多帖子。

当用户单击btnValidate按钮时,将调用Web服务。这是代码:

$("#btnValidate").click(function() {
// Creating variables to hold data from textboxes
var uname = $("#user").val();
var upass = $("#pass").val();
$.post("Authorize.php",
    { data: JSON.stringify({ LoginName: uname,Password: upass }) },
    function( data )
    {
        var result = JSON.parse( data );
        if ( result != null && result.Status != null && result.Status == 0 )
        {
            // if the result was valid and the Status was 0 ("success") then login succeeded
            location.href = "listAll.php";
            return;
        }
        // analyze what went wrong:
        if ( result == null )
        {
            alert( "Invalid JSON result from validation request" );
        } else if ( result.Status == null ) {
            alert( "No Status field found in JSON result from validation request" );
        } else {
            alert( "Invalid username or password. Please try again." );
        }
     }
    );
});
</script>

作为回报的Web服务,使用以下值进行响应:

{
    "Value": {
        "TokenId": 11,
        "LoginName": "Jerry.Mann",
        "Created": "2013-12-10T15:44:49",
        "Expires": "2013-12-31T15:44:49",
        "Token": "61f5da50-d0d9-4326-a403-f8e4798c0f0d",
        "LastUsed": "2013-12-10T15:44:49"
    },
    "Status": 0,
    "Message": null
}

到目前为止还不错。然而,我们想做的是将TokenId、LoginName、Created、Expires、Token、LastUsed中的值传递给一个名为listAll.php的页面。

将这些值传递到listAll.php的想法是,我们可以处理它们以及其他表单字段并提交到数据库。

我花了很多时间在谷歌上搜索答案,但到目前为止还没有找到任何可用的解决方案。

我最接近的解决方案是数据。价值代币

很明显,因为JSON结构在一个名为data的变量中,但我还没有弄清楚如何将其传递到listAll.php页面。

除此之外,还有数据。Value。Token将只传递tokenId的值。

任何关于如何将与数据变量相关的值传递到listAll.php的想法都将不胜感激。

  <form id="Form1" method="get" >
    <table cellpadding="0" cellspacing="0" border="0">
      <tr>
              <td class="label" nowrap><label for="user">UserName:</label></td>
                <td class="field">
                  <input  maxlength="40" class="required" name="user" id="user" size="20" type="text" tabindex="2" value="" style="width:400px;color:#999;font-size:9pt;height:20px;" />
                </td>
              </tr>
              <tr>
                <td class="label" nowrap><label for="pass">Password:</label></td>
                <td class="field">
                  <input  maxlength="40" class="required" name="pass" id="pass" size="20" type="text" tabindex="3" value="" style="width:400px;color:#999;font-size:9pt;height:20px;" />
                </td>
      </tr>
      <tr>
        <td></td>
        <td>
          <div class="buttonSubmit">
            <span></span>
                        <input type="button" id="btnValidate"
                            style="width: 180px" value="Log In" />
          </div><br clear="all"/>
        </td>
      </tr>
    </table>

在后端脚本Authorize.php中,您应该将令牌存储到会话中。

js

$("#btnValidate").click(function(){
    $.post('Authorize.php', {$('#form').serialize()}, function(data) {
        try {
            data = JSON.parse(data);
        } catch (e) {
            return false;
        }
        if (data && 'ok' == data.result) {
            top.location = 'listAll.php';
        }
        return false;
    });
});

Authorize.php

if (empty($_POST) || isset($_POST['pass']) || !isset($_POST['login'])) {
    exit;
}
/*
 * some authorize logic
 */
if ($authorized) {
    session_start();
    $_SESSION['token'] = $token;
    echo json_encode(array('result' => 'ok'));
}
exit;

您可以使用session将数组传递到另一个页面。

session_start();
$JsonResult = $_SESSION['result']
相关文章: