Javascript 为什么AJAX从PHP得到的响应有HTML标记?
我的响应文本有html标记,这会导致触发错误而不是成功。我做错了什么 以下是我的html代码:Javascript 为什么AJAX从PHP得到的响应有HTML标记?,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我的响应文本有html标记,这会导致触发错误而不是成功。我做错了什么 以下是我的html代码: <head> <title>HTML Window</title> <meta charset="UTF-8"> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script> fun
<head>
<title>HTML Window</title>
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
function post() {
var email = document.getElementById("email");
var password = document.getElementById("password");
var data = {
email: email.value,
password: password.value
};
$.ajax({
type: "GET",
url: "http://localhost/ParentChild/index.php",
dataType: "json",
data: data,
success: function (response) {
console.log("SUCCESS");
console.log(response);
},
error: function (response, textStatus, errorThrown) {
console.log("ERROR");
console.log("Status: " + textStatus);
console.log(response.responseText);
}
});
}
</script>
</head>
<body>
<form method="GET" action="javascript:post();">
<div>
Parent Window
<br><label for="email">Email: <input type="text" id="email" name="email"></label>
<br><label for="password">Password: <input type="text" id="password" name="password"></label>
<br><input type="submit" value="Post">
</div>
</form>
</body>
HTML窗口
职能职位(){
var email=document.getElementById(“电子邮件”);
var password=document.getElementById(“密码”);
风险值数据={
email:email.value,
密码:password.value
};
$.ajax({
键入:“获取”,
url:“http://localhost/ParentChild/index.php",
数据类型:“json”,
数据:数据,
成功:功能(响应){
控制台日志(“成功”);
控制台日志(响应);
},
错误:函数(响应、文本状态、错误抛出){
控制台日志(“错误”);
console.log(“状态:+textStatus”);
console.log(response.responseText);
}
});
}
父窗口
电邮:
密码:
以下是我的php:
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$response = array(
"a" => "test");
echo (json_encode($response);
?>
</body>
</html>
将数据返回到ajax调用的PHP脚本应该像子例程一样对待,而不是网页。如果该脚本中包含HTML,它也将作为总响应的一部分发送回浏览器
因此,修改脚本,即删除其中的所有HTML
<?php
$response = array("a" => "test");
echo (json_encode($response);
?>
这是因为您的php代码段将结果嵌入到html页面中。因为您的php中有这些结果……只需让您的其他php文件包含标题内容json和echo json encode ResponseAnks all……现在我明白了!
<?php
$response = array("a" => "test");
echo (json_encode($response);
?>