Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检索用于身份验证的JSON数据_Php_Jquery_Ajax_Json_Login - Fatal编程技术网

Php 检索用于身份验证的JSON数据

Php 检索用于身份验证的JSON数据,php,jquery,ajax,json,login,Php,Jquery,Ajax,Json,Login,我正在尝试使用php进行登录身份验证,以匹配用户名和密码,并将其作为JSON对象进行回显 表单将使用ajax发送,并读取返回的JSON数据。这是我遇到麻烦的部分 我的html如下所示: <form id='logform' method='post' > <label for="username">User Name:&nbsp;</label><input id='user_name' type='text' name='username

我正在尝试使用php进行登录身份验证,以匹配用户名和密码,并将其作为JSON对象进行回显

表单将使用ajax发送,并读取返回的JSON数据。这是我遇到麻烦的部分

我的html如下所示:

<form id='logform' method='post' >
   <label for="username">User Name:&nbsp;</label><input id='user_name' type='text' name='username' required placeholder='User Name' />
   <label for="password">Password:&nbsp;</label><input type='password' placeholder="Password" name='password' id="password" required />
   <input type="submit" name="submit" id="login" value="Log In"  />
   <input type="button" id="cancel_hide" value="Cancel" />
 </form>

上面的代码通过用户名和密码发送,当我在控制台日志中检查响应时,它会显示查询表中的数据。我想知道如何检索此数据以将其设置在会话存储中???

您的成功函数应该如下所示
success:function(data){…

然后数据是JSON对象,但作为字符串。然后使用
var object=JSON.parse(data)
检索实际对象

然后,您可以通过寻址新的
对象
变量来执行任何操作

编辑

success: function(data) {
    try {
        object = $.parseJSON(data);
    } catch (e) {
        // here you didn't get JSON back so we can assume it was an error, run your error code in here.  data will still be the error number (3)
        return false;
    }
    //run your code on the json object here.
}
for (var key in object) {
    //this if just checks if the key has a value, it is required
    if (object.hasOwnProperty(key)) {
        var value=object[key];
        //then we need to put the key and the value into session storage
        sessionStorage.setItem(key, value)
    }
}
在对象中循环

success: function(data) {
    try {
        object = $.parseJSON(data);
    } catch (e) {
        // here you didn't get JSON back so we can assume it was an error, run your error code in here.  data will still be the error number (3)
        return false;
    }
    //run your code on the json object here.
}
for (var key in object) {
    //this if just checks if the key has a value, it is required
    if (object.hasOwnProperty(key)) {
        var value=object[key];
        //then we need to put the key and the value into session storage
        sessionStorage.setItem(key, value)
    }
}

你能给我举个例子说明如何做到这一点吗?我更改了success函数,但不确定如何访问json数据或设置我的if语句:
success:function(data){var object=json.parse(data)if(data=='???')){
据我所知,
数据将以JSON字符串的形式返回,或者只返回单个整数
3
,在这种情况下,我们可以这样做。很抱歉,我对此有点陌生,我尝试了您建议的方法,当我
警报(数据)
时,我得到的结果是“object object”。我希望检索json对象中的数据,并在从服务器返回json时使用if语句来执行函数。我是否足够清楚?谢谢!现在如何将项目从数据设置到会话存储?我注意到,由于我实现了上述功能,即使密码不正确,它也会运行。不要混合返回数据的数据类型…在两种服务器端情况下都使用json您的确切意思是什么?