[AJAX]原生AJAX——服务端如何发出JSON格式响应,客户端如何处理接收JSON格式响应

news/2025/2/15 6:02:57/

服务端代码:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body></body>
<script>// 1、创建对象const xhr = new XMLHttpRequest();// 2、初始化:设置请求类型和urlxhr.open('POST', 'http://127.0.0.1:8000/server');// 设置请求头// Content-Type:设置请求体内容类型// application/x-www-form-urlencoded:请求参数的类型xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');// 3、发送:设置请求体(POST请求的参数)xhr.send("id:1&name:CUYG")// 4、事件绑定xhr.onreadystatechange = function () {// 判断if (xhr.readyState === 4) {if (xhr.status >= 200 && xhr.status < 300) {console.log(xhr.response);}}};
</script></html>

主要代码

// 第一步:设置发给客户端的JSON格式数据

    var data={

        code:200,

        msg:"成功"

    }

    // 第二步:由于response.send()只能发送字符串,所以要把JSON转换成字符串

    data = JSON.stringify(data)

    // 第三步:发送数据

    response.send(data);

根据上图表示客户端接收到数据,打印出的响应参数是字符串

 方法一:使用JSON.parse(xhr.response)把字符串转换为JSON

<script>// 1、创建对象const xhr = new XMLHttpRequest();// 2、初始化:设置请求类型和urlxhr.open('POST', 'http://127.0.0.1:8000/server');// 设置请求头// Content-Type:设置请求体内容类型// application/x-www-form-urlencoded:请求参数的类型xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');// 3、发送:设置请求体(POST请求的参数)xhr.send("id:1&name:CUYG")// 4、事件绑定xhr.onreadystatechange = function () {// 判断if (xhr.readyState === 4) {if (xhr.status >= 200 && xhr.status < 300) {console.log(JSON.parse(xhr.response));}}};
</script>

方法二:设置响应体数据类型:xhr.responseType="json"

<script>// 1、创建对象const xhr = new XMLHttpRequest();// 2、初始化:设置请求类型和urlxhr.open('POST', 'http://127.0.0.1:8000/server');// 设置请求头// Content-Type:设置请求体内容类型// application/x-www-form-urlencoded:请求参数的类型xhr.responseType="json"xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');// 3、发送:设置请求体(POST请求的参数)xhr.send("id:1&name:CUYG")// 4、事件绑定xhr.onreadystatechange = function () {// 判断if (xhr.readyState === 4) {if (xhr.status >= 200 && xhr.status < 300) {console.log(xhr.response);}}};
</script>

 


http://www.ppmy.cn/news/735538.html

相关文章

i5 1135g7和i7 10510u 哪个好

i5-1135G7 4 核 8 线程&#xff0c;主频 2.4GHz&#xff0c;睿频 4.2GHz&#xff0c;8MB 三级缓存。跑分方面&#xff0c;i5-1135G7 在 Geekbench 4 上单核最高 6046 分&#xff0c;多核最高 21469 分。 选i7 10510u还是 i5 1135g7这些点很重要!看完你就知道了 https://list.jd…

i5 10200h和i7 10750h 哪个好

i5-10200H的酷睿处理器&#xff0c;它采用4核心8线程&#xff0c;和我们熟悉的i5-10300H相比&#xff0c;默认主频从2.5GHz降为了2.4GHz&#xff0c;最高&#xff08;单核&#xff09;睿频加速频率从4.5GHz降为了4.1GHz&#xff0c;综合性能下降了大约10%不到&#xff0c;但却换…

i5-1135G7和i7-1165G7参数对比哪个好

i5-1135G7 4 核 8 线程&#xff0c;主频 2.4GHz&#xff0c;睿频 4.2GHz&#xff0c;8MB 三级缓存。跑分方面&#xff0c;i5-1135G7 在 Geekbench 4 上单核最高 6046 分&#xff0c;多核最高 21469 分。 选i5-1135G7和i7-1165G7搭配笔记本这些点很重要 https://list.jd.com/lis…

i5 1130g7和 i7 1160g7哪个好

i5-1130G7 4 核 8 线程&#xff0c;主频 2.4GHz&#xff0c;睿频 4.00GHz&#xff0c;8MB 三级缓存。跑分方面&#xff0c;i5-1130G7 在 Geekbench 4 上单核最高 5946 分&#xff0c;多核最高 20469 分。 选i5 1130g7还是 i7 1160g7 这些点很重要看过你就懂了 http://www.adian…

达人评测 i7 11390h和i5 11320h选哪个好

i7 11390H 属于Tiger Lake-H35系列为4核心8线程&#xff0c;三缓12MB&#xff0c;不过基频更高&#xff0c;达到了3.39GHz&#xff0c;。i7-11390H单核睿频最高可达5GHz&#xff0c;TDP在28-35W之间选i7 11390h还是i5 11320h这些点很重要http://www.adiannao.cn/dy i5-11320h参…

i5 11300h和i7 1165g7哪个好 i5 11300h和i71165g7的差距多大

i5-1135G7 4 核 8 线程&#xff0c;主频 2.4GHz&#xff0c;睿频 4.2GHz&#xff0c;8MB 三级缓存。跑分方面&#xff0c;i5-1135G7 在 Geekbench 4 上单核最高 6046 分&#xff0c;多核最高 21469 分。选i7 1165g7还是i5 11300h哪个好这些点很重要 http://www.adiannao.cn/dy …

亚马逊Web服务(AWS)的几个主要应用

以下是亚马逊Web服务&#xff08;AWS&#xff09;的几个主要应用&#xff1a; EC2&#xff08;弹性计算云&#xff09;&#xff1a;提供虚拟计算资源&#xff0c;可为应用程序和服务提供计算能力。 S3&#xff08;简单存储服务&#xff09;&#xff1a;提供高度可扩展和可靠的…

安装部署mysql

1.二进制方式安装 (1)下载压缩包 (2)创建用户和组 (3)解压 &#xff08;4&#xff09;创建软链接 (5)初始化 &#xff08;6&#xff09; 2.使用yum方式安装 (1)用yum install wget unzip bash-completion tree vim lrzsz net-tools -y命令安装常用软件 (2)用yum install mysql…