我正在使用php和MSSQL开发一个web服务。我可以连接db和retrievev值。但是当我使用json时,它将不返回任何值。
用于Web服务的ajax代码是
$.ajax({
url: 'http://209.249.81.138/dev/logintalk.php',
type: 'POST',
data: "username=" + $("#username").val()+"&password=" + $("#password").val(),
dataType: 'json',
success: function(data){
alert(data);
},
error: function(){
alert("failure...");
}
});
}
使用的php代码是
<?php
$server = "server";
$username = "username";
$password = "secret";
$database = "db";
$con = mssql_connect($server, $username, $password) or die("Couldn't connect to SQL Server on $Server");
$users = $_POST['username'];
$passwd = $_POST['password'];
$selected = mssql_select_db($database, $con) or die("Couldn't open database $database");
mssql_query('SET CHARACTER SET utf8');
$query = "SELECT * FROM zNewUsers WHERE memberid = '$users' AND userid = '$passwd'";
$result = mssql_query($query) or die ("Unable to verify user because ");
$row = mssql_fetch_array($result);
$count = mssql_num_rows($result);
if($count == 0){
$data=array(0=>"Incorrect values...");
$value[]=$data[0];
echo json_encode($value);
}
else{
$val[]=$row['SessionID'];
echo json_encode($val);
}
mssql_close($con);
?>
答案 0 :(得分:0)
几个疑问之外的问题:
success: function(data){
console.log(data);
//This is better for debugging if you use firebug, if you don't do it.
},
if($count == 0){
echo json_encode(array(0=>"Incorrect values..."));
//Why create new variables? Pass what you want directly to json_encode()
}
else{
echo json_encode($row['SessionID']);
}
回到问题:
var_dump()您希望在编码之前编码的数据,并确保它没问题,然后通过console.log(data)
检查您在浏览器中获得的内容,并与我们分享此信息,以便我们提供更好的帮助。