【发布时间】:2016-02-15 20:19:13
【问题描述】:
我有一个jsonp的脚本如下
<script>
$(document).ready(function(){
$("#LoginForm").submit(function(){
var data = $(this).serialize();
//alert(data);
$.ajax({
type:"POST",
dataType:"jsonp",
url:"https://akshay.tk/api/login.php",
data:data,
success:function(data)
{
/// WHAT TO WRITE HER TO GET PHP RESPONSE
/// DIV WHERE DATA TO BE SHOWN, ID IS RESULT
}
});
return false;
});
});
</script>
我的php代码是
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
// login check for user
$result = mysqli_query($con,"SELECT * FROM `admin_login` WHERE `username`='$username' AND `password`='$password'");
if(mysqli_num_rows($result) > 0)
{
$user = mysqli_fetch_assoc($result);
// user found
$response["success"] = 1;
$response["uid"] = $user["id"];
$response["username"] = $user["username"];
$response["role"] = $user["role"];
echo json_encode($response);
}
一切都很顺利,当我使用开发人员工具时,我才知道它也给出了正确的响应。 php回复:
{"success":1,"uid":"1","username":"admin","role":"admin"}
我应该在 jQuery SUCCESS 函数中编写什么代码才能获得 php 响应?
【问题讨论】:
标签: php jquery json ajax jsonp