Ajax登录页面返回html标记
问题描述:
我想使用ajax和php做一个简单的登录页面。它返回的响应文字是这样的: 的responseText:Ajax登录页面返回html标记
<html>
<head>
</head>
<body>
</body>
</html>
success
这究竟是为什么?我如何摆脱这些html标签。 我的代码是:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.js"></script>
</head>
<body>
<form action="" method="post" id="myform">
<label>Username</label><input type="text" id="username" name="user">
<label>Password</label><input type="text" id="password" name="pwd">
<input type="button" onclick="sub()" value="Submit">
</form>
</body>
</html>
<script>
function sub()
{
var email=$("#username").val();
var password=$("#password").val();
var dataString = 'email='+email+'&password='+password;
if($.trim(email).length>0 && $.trim(password).length>0)
{
$.ajax({
type: "POST",
dataType: "text",
url: "home1.php",
data: "username=" + $("#username").val()+"&password=" + $("#email").val(),
cache: false,
success: function(status){
alert(status);
}
});
}
}
</script>
home1.php:
<?php
include("db.php");
session_start();
if(isSet($_POST['email']) && isSet($_POST['password']))
{
$email=mysqli_real_escape_string($db,$_POST['email']);
$password=mysqli_real_escape_string($db,$_POST['password']);
$result=mysqli_query($db,"SELECT user_id FROM customers WHERE email1='$email' and password='$password'");
$count=mysqli_num_rows($result);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if($count==1)
{
$_SESSION['id']=$row['user_id'];
echo "success";
}
}
?>
答
你home1.php页面必须返回一个文本,如果你想警报(状态)才能正常工作。
可能的是,这些标记可能会被您的Web服务器插入,因为您没有正确设置Content-Type。
<?php
// home1.php
Header('Content-Type: text/plain;charset=utf8');
die("This is the response");
以上应该与您的代码一起工作。
对于更高级的例子,尝试删除数据类型“文本”,并使用JSON:
success: function(response) {
if (response.hasOwnProperty('status')) {
// We should actually check there is a message property too...
alert(response.message);
} else {
alert('Incorrect response received');
}
}
和home1.php开始只包含一个文件:
<?php
$response = array(
'status' => 'success',
'message' => 'Everything is OK!',
);
Header('Content-Type: application/json;charset=utf8');
die(json_encode($response));
,看是否它一切正常。
此外
你使用jQuery,这样可以节省一些编码:
function sub() {
var email=$("#username").val();
var password=$("#password").val();
var dataString = 'email='+email+'&password='+password;
if($.trim(email).length>0 && $.trim(password).length>0) {
$.post(
'home1.php',
{
username: email,
password: password
}
).done(function(response) {
// Here response is the JSON object returned by home1.php
});
+0
尝试添加 标题('Content-Type:text/plain; charset = utf8'); die(“这是回应”); 仍然无法正常工作。甚至用json试了一下..也没有运气。 – Divya
答
改变你的函数这个
$('#myform').submit(function(event){
event.preventDefault();
var username = $.trim($("#username").val()),
password = $("#password").val();//don't trim passwords
if (username.length > 0 && password.length > 0) {
$.ajax({
method: "POST",
url: "home1.php",
data: { pUsername: username, pPassword: password }
}).done(function(data) {
alert(data); //shows all data received from home1.php
});
}
});
人们可以更好地帮助,如果你的代码是正确格式化 – CoffeeNinja
您可能还想包含PHP文件的来源,因为它是返回标记的。 –
这里和php有什么关系?另外,任何地方都没有'电子邮件ID'。 –