Ajax接收五种响应消息

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);
?>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值