1.text/plain 接受的字符串文件
客户端:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
doresponse(xhr);
}
};
xhr.open("GET", "1.php",true);
xhr.send(null);
function doresponse() {
console.log(xhr.responseText);
}
服务端:
header('Content-Type:text/plain;charset=UTF-8');
echo '字符串';
2.text/html
html标签组成的字符串片段 Ajax请求中返回的不是完整的HTML文档.
客户端:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
doresponse(xhr);
}
};
xhr.open("GET", "1.php",true);
xhr.send(null);
function doresponse() {
document.getElementsByTagName("body")[0].innerHTML= xhr.responseText;
}
服务端:
<?php
header('Content-Type:text/html;charset=UTF-8');
echo '<span>html</span>';
?>
3.application/javascript
根据JavaScript语法拼接的字符串
客户端:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
doresponse(xhr);
}
};
xhr.open("GET", "1.php",true);
xhr.send(null);
function doresponse() {
eval(xhr.responseText);
}
}
服务端:
<?php
header('Content-Type:application/javascript;charset:UTF-8');
echo 'var p = document.createElement("p");p.innerHTML = "js方式";document.body.appendChild(p)';
?>
4.appliaction/xml格式
客户端:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
doresponse(xhr);
}
};
xhr.open("GET", "1.php",true);
xhr.send(null);
function doresponse() {
document.getElementsByTagName("body")[0].innerHTML= xhr.responseText;
}
}
服务端:
<?php
header('Content-Type:application/xml');
$str = "<?xml version='1.0' encoding='UTF-8'?>";
$str .= "<zc>xml方式</zc>";
echo $str;
?>
5.application/json格式
客户端:
<ul id="suggests">
</ul>
<script>
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
doresponse(xhr);
}
};
xhr.open("GET", "1.php",true);
xhr.send(null);
function doresponse() {
if(xhr.responseText){
var stuList = JSON.parse(xhr.responseText);
var str = "";
for(var i = 0;i<stuList.length;i++){
var stu = stuList[i];
str +=`
<li>${stu['stuName']}</li>
`;
}
suggests.innerHTML = str;
}
}
</script>
服务端:
<?php
header('Content-Type:application/json;charset=UTF-8');
$list = [
['stuId' => 1000,'stuName' => '张三','stuAge' => 18,'stuSex' => 1,'className' =>'高三二班'],
['stuId' => 1001,'stuName' => '李四','stuAge' => 18,'stuSex' => 1,'className' =>'高三二班'],
['stuId' => 1002,'stuName' => '王五','stuAge' => 17,'stuSex' => 0,'className' =>'高三二班'],
['stuId' => 1003,'stuName' => '赵六','stuAge' => 18,'stuSex' => 1,'className' =>'高三二班'],
['stuId' => 1004,'stuName' => '张三丰','stuAge' => 16,'stuSex' => 1,'className' =>'高二一班'],
['stuId' => 1005,'stuName' => '张无忌','stuAge' => 17,'stuSex' => 1,'className' =>'高二一班'],
['stuId' => 1006,'stuName' => '周芷若','stuAge' => 16,'stuSex' => 1,'className' =>'高二一班'],
['stuId' => 1007,'stuName' => '赵敏','stuAge' => 16,'stuSex' => 1,'className' =>'高二一班'],
['stuId' => 1008,'stuName' => '小昭','stuAge' => 14,'stuSex' => 0,'className' =>'高一一班'],
['stuId' => 1009,'stuName' => '杨过','stuAge' => 20,'stuSex' => 1,'className' =>'高高一班']
];
echo json_encode($list);
?>