您好,我正在使用一个带ajax的登录脚本,我想回调并显示我的数据email和usernam,以便将其存储在本地存储中。我可以用json格式获取数据,但我想用console.log格式显示这些数据
这是我的代码
send_ajax.js
   $(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
<?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).'}';
                    }
            }
?>发布于 2014-12-26 04:39:25
为了从后台发送响应,你需要在json中格式化你的数据。一旦得到响应,就需要使用parseJSON()和you can菜单。
https://stackoverflow.com/questions/27603361
复制相似问题