首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >显示来自post jquery的json数据

显示来自post jquery的json数据
EN

Stack Overflow用户
提问于 2014-12-22 21:07:42
回答 3查看 746关注 0票数 0

您好,我正在使用一个带ajax的登录脚本,我想回调并显示我的数据emailusernam,以便将其存储在本地存储中。我可以用json格式获取数据,但我想用console.log格式显示这些数据

这是我的代码

send_ajax.js

代码语言:javascript
运行
复制
   $(document).ready(function(e) {
$("#contactSubmitButton").click(function(){
        var email    = $("#contactEmailField").val();
        var password = $("#contactNameField").val();

    $.ajax({
    type: "POST",
    url: "http://hubafrica.co/webservices/get_user.php",
    data: "email="+email+"&password="+password,
    dataType: "json",
    cache: false,

    beforeSend: function(){ $("#contactSubmitButton").val('Chargement...');},

    success: function(data) {
        alert(data);

        if(data)
        {

             iterateJson(data);
             var url="http://hubafrica.co/webservices/get_user.php";

                    $.get(url,function(data){

                    // loop through the members here

                    $.each(json.data,function(i,dat){

                        console.log(dat.email);

                        window.localStorage.setItem("id", dat.id);


                    });

                  });



        //window.location.href = "user_dashboard.html";

        }else{

            $("#formSuccessMessageWrap").fadeIn('slow');
            $("#contactSubmitButton").val('se connecter');



        }


    },
        error: function (xhr, ajaxOptions, thrownError) {


      }
      });
    });
});

script.php

代码语言:javascript
运行
复制
<?php 

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

header('Access-Control-Allow-Origin: *');

include("../config/config.php");

$account=array();

if (isset($_POST["email"])){

                $email = htmlspecialchars($_POST["email"]);
                $pass =  htmlspecialchars($_POST["password"]);
                    $sql = mysql_query('SELECT * FROM `b2b_user` where email="'.$email.'" and password="'.$pass.'"');
                    $num = mysql_num_rows($sql);

                    if($num > 0){


                        $row = mysql_fetch_assoc($sql);
                        $account['id']  = $row['id'];
                        $account['email'] = $row['email'];
                        echo '{"members":'.json_encode($account).'}';

                    }

            }
?>
EN

回答 3

Stack Overflow用户

发布于 2014-12-26 04:39:25

为了从后台发送响应,你需要在json中格式化你的数据。一旦得到响应,就需要使用parseJSON()和you can菜单。

票数 1
EN

Stack Overflow用户

发布于 2014-12-22 21:24:14

此处更改:使用data.members访问数据:

在你的php结构中,如下所示:不要追加字符串。

代码语言:javascript
运行
复制
 $account1= array('members'=>$account);
 echo json_encode($account1);

脚本:

代码语言:javascript
运行
复制
$.each(data.members,function(i,dat){

                        console.log(dat.email);

                        window.localStorage.setItem("id", dat.id);


                    });
票数 0
EN

Stack Overflow用户

发布于 2014-12-22 23:39:17

您可以使用

代码语言:javascript
运行
复制
 alert(JSON.stringify(data));

代码语言:javascript
运行
复制
 Console.log(JSON.stringify(data));

下面这行代码将对象/数组转换为JSON文本,并将该JSON文本存储在字符串中。

代码语言:javascript
运行
复制
 JSON.stringify('/array variable here/')

希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27603361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档