您好,我正在使用一个带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菜单。
发布于 2014-12-22 21:24:14
此处更改:使用data.members访问数据:
在你的php结构中,如下所示:不要追加字符串。
$account1= array('members'=>$account);
echo json_encode($account1);脚本:
$.each(data.members,function(i,dat){
console.log(dat.email);
window.localStorage.setItem("id", dat.id);
});发布于 2014-12-22 23:39:17
您可以使用
alert(JSON.stringify(data));或
Console.log(JSON.stringify(data));下面这行代码将对象/数组转换为JSON文本,并将该JSON文本存储在字符串中。
JSON.stringify('/array variable here/')希望能有所帮助。
https://stackoverflow.com/questions/27603361
复制相似问题