成功函数?它总是跳转到错误函数??请告诉我这个代码有什么问题!! // jquery ajax代码
$.ajax({
type: 'GET',
dataType: 'json',
async:false,
contentType:'application/json', data:'fname1='+$('#fname').val()+'&lname1='+$('#lname').val()+'&email1='+$('#email').val()+'&contact1='+$('#contact').val()+'&password1='+$('#password').val(),
beforeSend: function(x){
if(x && x.overrideMimeType){ x.overrideMimeType("application/json;charset=UTF-8");
}
},
url: "./register.php",
success:function(data) {
if(data1.success=="true")
{
$('#register_response').html("<img src='images/36.gif' alt='Signing Up..' />");
$('#dialog').fadeOut('slow');
$('#overlay').fadeOut('slow');
window.location="index.php";
}
},
error: function(data) {
console.log(textStatus, errorThrown); }
});
这是我的PHP代码,其中m接受json数据!!
// php code
<?php
$fname=$_POST['fname1'];
$lname=$_POST['lname1'];
$contact=$_POST['contact1'];
$email=$_POST['email1'];
$pass=$_POST['password1'];
require("connect.php");
$qry="SELECT * FROM register WHERE $email='$email'";
$result=mysql_query($qry,$con);
$count=mysql_num_rows($result);
if($count==0)
{
$data = array('success'=>"true");
echo json_encode($data);
$query="INSERT INTO register(fname,lname,contact,email,password)VALUES('$fname','$lname','$contact','$email','$pass')";
$rslt=mysql_query($query,$con);
setcookie("user",$email,time()+36000);
}
else
{
$data = array('success'=>"false");
echo json_encode($data);
}
mysql_close($con);
?>
答案 0 :(得分:1)
您的查询中有一个额外的$
,它应该是:
$qry="SELECT * FROM register WHERE email='$email'";
您需要添加错误检查,以便您看到以下错误:
$result = mysql_query($qry, $con) or die (mysql_error($con));
答案 1 :(得分:0)
如果您的返回值仅为true或false,则可以跳过使用json并尝试使用简单的jQuery.post或jQuery.get http://api.jquery.com/jQuery.get/
var data = 'fname1=' + $('#fname').val() + '&lname1=' + $('#lname').val() + '&email1=' + $('#email').val() + '&contact1=' + $('#contact').val() + '&password1=' + $('#password').val();
jQuery.get('/register/'+data,function(response){
if (response == "true") {
$('#register_response').html("<img src='images/36.gif' alt='Signing Up..' />");
$('#dialog').fadeOut('slow');
$('#overlay').fadeOut('slow');
window.location = "index.php";
}else{
//handle your error
}
});