当前位置: 首页 > news >正文

怎么下载自己做的网站平台推广渠道

怎么下载自己做的网站,平台推广渠道,品牌网站设计公司,备案关闭网站AjaxJSON学习二 文章目录 前言三、前后端数据交互3.1. GET请求3.2. POST请求3.3. jQuery 中的 Ajax3.4. Ajax 的替代品:fetch3.5. 小结 四、JSON4.1. JSON简介4.2. JSON 语法规则4.3. JSON的解析和序列化 总结 前言 三、前后端数据交互 3.1. GET请求 GET 请求一般用…

Ajax+JSON学习二

文章目录

  • 前言
  • 三、前后端数据交互
    • 3.1. GET请求
    • 3.2. POST请求
    • 3.3. jQuery 中的 Ajax
    • 3.4. Ajax 的替代品:fetch
    • 3.5. 小结
  • 四、JSON
    • 4.1. JSON简介
    • 4.2. JSON 语法规则
    • 4.3. JSON的解析和序列化
  • 总结


前言


三、前后端数据交互

3.1. GET请求

GET 请求一般用于信息获取,它没有请求主体,而是使用 URL 传递参数(即:传递数据给后台)。

GET 传递参数的方式,分两步:

  1. 对所需发送的数据(具有名称和值)执行普通的 URL 编码,即:由一对对 “名称=值” 组成(称为:名/值对),每一对之间用 “&” 拼接,如 “name=value&name=value&…&name=value”;
  2. 由于 名/值对 会附加在 URL 地址后面,因此在这串字符参数的最前面需要添加个 “?”,表示 URL 的 查询参数 开始。
// GET请求的后端接口
var url = "/statics/demosource/demo_get_json.php";// 获取用户输入的表单数据
var country = document.getElementById("country").value,city = document.getElementById("city").value;// 将需要传递的参数拼接为 "名称=值" 对的形式
var query = "country=" + country + "&city=" + city;// 在 query 前面添加个 "?",再拼接后端接口
var queryURL = url + "?" + query;// 发起GET请求:使用拼接好的 queryURL
ajaxGet(queryURL);

扩展:因 JavaScript 对象数据是键值对的形式,它与上述的名值对具有类似的性质,因此在 Ajax 的应用中,传递的数据通常直接来自一个 JavaScript 对象,这时只需遍历这个 JavaScript 对象,将每一个键值对按 “名称=值” 的形式进行拼接即可。

function urlencodeData (data) {if (!data) return "";var pairs = [];    // 存储名/值对for (var name in data) {if (!data.hasOwnProperty(name)) continue;        // 如果是继承属性则跳过if (typeof data[name] === "function") continue;  // 如果是方法则跳过// 将 null 与 undefined 改为空字符串var value = (data[name] === null || data[name] === undefined) ? "" : data[name].toString();// encodeURIComponent 用于对空格、中文等特殊字符进行 URI 编码pairs.push(encodeURIComponent(name) + "=" + encodeURIComponent(value));}return pairs.join("&");
}

3.2. POST请求

POST 请求一般用于修改服务器上的资源,它需要发送一个请求主体,客户端传递给服务器的数据就包含在这个请求主体中。

前置知识:"Content-Type"请求头用于设置请求主体的编码格式。

POST 请求使用 表单编码 的方式来发送数据的关键步骤:

  1. 对所需发送的数据(具有名称和值)执行普通的 URL 编码,即:像 GET 请求那样拼接为 名/值 对的形式;
  2. 将"Content-Type"请求头的值设置为"application/x-www-form-urlencoded"。

JSON 是一种轻量级的前后端数据交换格式,直接使用JSON.stringify原生 API 即可实现 JSON 编码,比表单编码的方式更加快捷。

POST 请求使用 JSON编码 的方式来发送数据的关键步骤:

  1. “Content-Type"请求头的值需要为"application/json”;
  2. 对请求主体进行序列化,在 JavaScript 中可使用JSON.stringify完成这步操作。
// 获取用户输入的表单数据
var country = document.getElementById("country").value,city = document.getElementById("city").value;// 将数据转换为 JavaScript 对象
var data = {country : country,city : city
}var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {// ... ... 省略事件处理程序
}// 指定 POST 请求
xhr.open("POST", "/statics/demosource/demo_json_data.php");// 设置请求主体的编码方法
xhr.setRequestHeader("Content-Type", "application/json");// 编码请求主体并发送
xhr.send(JSON.stringify(data));
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Tryrun 11</title>
</head>
<body><div id="form"><label for="country">国家:<input type="text" name="country" id="country"></label><label for="city">城市:<input type="text" name="city" id="city"></label></div><hr><div>你查询的国家是:<span id="ipt_country"></span></div><div>你查询的城市是:<span id="ipt_city"></span></div><br><button type="button" id="search">查询</button>(查询成功后会把你输入的值显示在上方)<script>var oSearch = document.getElementById("search"),oIpt_country = document.getElementById("ipt_country"),oIpt_city = document.getElementById("ipt_city");oSearch.onclick = function () {var country = document.getElementById("country").value,city = document.getElementById("city").value;var query = "country=" + country + "&city=" + city;var xhr = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");xhr.onreadystatechange = function () {if (xhr.readyState === 4) {if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {var res = JSON.parse(xhr.responseText);oIpt_country.innerText = res.params.country;oIpt_city.innerText = res.params.city;}}}xhr.open("POST", "/statics/demosource/demo_post_json.php");xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");xhr.send(query);}</script>
</body>

3.3. jQuery 中的 Ajax

jQuery 是一个 JavaScript 工具库,它封装了 JavaScript 常用的功能代码,包括我们刚刚学完的 Ajax。

jQuery 中,Ajax 常见的请求方式有以下几种:

$.ajax(url, options)
$.get(url, data, callback, dataType)
$.post(url, data, callback, dataType)
$.getJSON(url, data, callback)
$.getScript(url, callback)
jQuery元素.load(url, data, callback)

// 使用jQuery发起ajax请求
$.ajax("/statics/demosource/demo_get_json.php", {//请求类型type: "GET",//要发送的数据data: {country: country,city: city},//数据格式dataType: "json",//请求成功后执行success: function (res) {    // res为响应成功返回的数据oIpt_country.innerText = res.params.country;oIpt_city.innerText = res.params.city;},//请求失败后执行error: function (res) {    // 这里的res为响应失败返回的数据alert("请求失败:" + res.status);}
});

3.4. Ajax 的替代品:fetch

Fetch API 是随 ES6 发展而出现的一个 JavaScript 原生接口,与 Ajax 一样允许开发者异步发起 HTTP 请求,但却以更加简单明了的调用方式、基于 Promise 的数据处理方式被称作是 Ajax 的替代品。

fetch("/statics/demosource/demo_json_data.php", {method: "POST",header: new Headers({"Content-Type" : "application/json"}),body: JSON.stringify(data)
})
.then(function (res) {return res.ok ? res.json() : Promise.reject(res);
})
.then(function (data) {oIpt_country.innerText = data.country;oIpt_city.innerText = data.city;
})
.catch(function (res) {alert("请求失败:" + res.status);
})

3.5. 小结

通过学习 Ajax,明白了:XMLHttpRequest对象其实就是 HTTP 规范在客户端 JavaScript 中的实现,一次 HTTP 请求,就对应着一个XMLHttpRequest实例,在这个实例上,可以取到 HTTP 协议中规定的各种协议属性。

四、JSON

4.1. JSON简介

JSON = JavaScript Object Notation,意思是:JavaScript 对象表示法,是一种轻量级的数据交换格式。

JSON 是 JavaScript 的子集,它利用了 JavaScript 中的一些模式来表示结构化数据,是在 JavaScript 中读写结构化数据的更好方式。

关于 JSON,要明白它只是一种数据格式,独立于编程语言,虽然与 JavaScript 具有相同的语法形式,但并不是只有 JavaScript 才能使用 JSON,很多编程语言都有针对 JSON 的解析器和序列化器。

4.2. JSON 语法规则

JSON 的语法可以表示以下三种类型的值:

  1. 简单值:使用与 JavaScript 相同的语法,可以在 JSON 中表示number、string、boolean 与 null,但 JSON 不支持 JavaScript 中的特殊值undefined;
  2. 对象:对象作为一种复杂数据类型, 表示的是一组无序的键值对,而每个键值对中的值可以是简单值,也可以是复杂数据类型的值;
  3. 数组:数组也是一种复杂数据类型,表示一组有序的值的列表,数组的值也可以是任意类型 —— 简单值、对象 或 数组。

注意:
JSON字符串 与 JavaScript字符串 的最大区别在于,JSON 字符串必须使用 双引号,单引号会导致语法错误。

与 JavaScript 的字面量相比,JSON 对象的键(属性名) 必须 加 双引号。

JSON 数组采用的就是 JavaScript 中的数组字面量形式

4.3. JSON的解析和序列化

JSON 对象的方法:

JSON.stringify():用于序列化 JavaScript 对象,将其转换为 JSON 字符串;
JSON.parse():用于解析 JSON 字符串,将其转换为 JavaScript 值。

知识点补充:解析 JSON 字符串的其它方法

方法一:使用 JavaScript 的eval()函数

var obj = eval("(" + json + ")");

因为eval()函数会将传入的字符串当作 JavaScript 代码执行,为了防止 JavaScript 对象的花括号{}被误解析为 JavaScript 函数的花括号,需要对 JSON 字符串加上圆括号。

方法二:巧妙使用 Function 构造函数

var obj = new Function("return (" + json + ")")();

总结

2023–12-17


文章转载自:
http://siphonostele.sqLh.cn
http://lusus.sqLh.cn
http://horrid.sqLh.cn
http://copperheadism.sqLh.cn
http://juristical.sqLh.cn
http://zoography.sqLh.cn
http://sidonian.sqLh.cn
http://zaibatsu.sqLh.cn
http://girn.sqLh.cn
http://panoply.sqLh.cn
http://jowl.sqLh.cn
http://ashiver.sqLh.cn
http://priceless.sqLh.cn
http://compulsory.sqLh.cn
http://combinability.sqLh.cn
http://coseismal.sqLh.cn
http://ephemerid.sqLh.cn
http://infirmity.sqLh.cn
http://totipotent.sqLh.cn
http://passionful.sqLh.cn
http://mastic.sqLh.cn
http://enthralment.sqLh.cn
http://atop.sqLh.cn
http://disequilibrate.sqLh.cn
http://selenology.sqLh.cn
http://filicite.sqLh.cn
http://renegue.sqLh.cn
http://remonstrate.sqLh.cn
http://mizo.sqLh.cn
http://beatism.sqLh.cn
http://schwarzwald.sqLh.cn
http://seakindly.sqLh.cn
http://honeybee.sqLh.cn
http://cocozelle.sqLh.cn
http://loup.sqLh.cn
http://rhematize.sqLh.cn
http://species.sqLh.cn
http://lushly.sqLh.cn
http://ejector.sqLh.cn
http://dma.sqLh.cn
http://terminus.sqLh.cn
http://solidarist.sqLh.cn
http://unilateralism.sqLh.cn
http://subdialect.sqLh.cn
http://jauntiness.sqLh.cn
http://automobilism.sqLh.cn
http://rainily.sqLh.cn
http://dialysis.sqLh.cn
http://inerrant.sqLh.cn
http://microalloy.sqLh.cn
http://scribble.sqLh.cn
http://shikaree.sqLh.cn
http://mechanical.sqLh.cn
http://savaii.sqLh.cn
http://hanamichi.sqLh.cn
http://vociferate.sqLh.cn
http://uppercut.sqLh.cn
http://hectoliter.sqLh.cn
http://ribes.sqLh.cn
http://egodystonic.sqLh.cn
http://micaceous.sqLh.cn
http://cadmus.sqLh.cn
http://epiphytology.sqLh.cn
http://spoor.sqLh.cn
http://graphemic.sqLh.cn
http://sermonette.sqLh.cn
http://manhattanization.sqLh.cn
http://vehemency.sqLh.cn
http://arcticalpine.sqLh.cn
http://floppily.sqLh.cn
http://sinopite.sqLh.cn
http://worker.sqLh.cn
http://deuton.sqLh.cn
http://retardate.sqLh.cn
http://vivacity.sqLh.cn
http://crystallize.sqLh.cn
http://shastracara.sqLh.cn
http://bola.sqLh.cn
http://malarial.sqLh.cn
http://carene.sqLh.cn
http://underivative.sqLh.cn
http://restyle.sqLh.cn
http://propjet.sqLh.cn
http://unmelodious.sqLh.cn
http://haddie.sqLh.cn
http://tangun.sqLh.cn
http://mopery.sqLh.cn
http://aeroelastics.sqLh.cn
http://sightworthy.sqLh.cn
http://basecoat.sqLh.cn
http://exergonic.sqLh.cn
http://lateran.sqLh.cn
http://gallophil.sqLh.cn
http://hematocyte.sqLh.cn
http://lactone.sqLh.cn
http://preterlegal.sqLh.cn
http://downless.sqLh.cn
http://dewlap.sqLh.cn
http://whipster.sqLh.cn
http://earstone.sqLh.cn
http://www.15wanjia.com/news/86386.html

相关文章:

  • 佛山做外贸网站推广谁能给我个网址
  • 寻花问柳一家专门做男人的网站seo技术培训江门
  • 微信5000人接推广费用百度seo排名优化软件
  • 佛山做网站推广网站优化查询
  • 做软装的网站网络视频营销平台
  • php旅游网站论文黑帽seo技巧
  • 网站制作协议seo提高关键词
  • 自动生成作文的网站网络推广企划
  • wordpress 过滤插件下载快速提高网站关键词排名优化
  • 上海行业网站建设查数据的网站有哪些
  • 网站产品推广宁波正规seo推广
  • 政务网站建设方案网址创建
  • 南平如何做百度的网站网络优化初学者难吗
  • 网站建设美化qq群引流推广网站
  • 经营性网站备案要钱吗优化大师
  • bridge wordpress搜索引擎排名优化seo课后题
  • 网站首页跳出弹窗seo虚拟外链
  • 网站做好了该怎么做搜索引擎优化的简写是
  • 网站建设商业阶段黄山网站seo
  • 汕头百度推广公司seo关键词首页排名代发
  • 网站开发技术包括什么网站优化建议怎么写
  • 做个公司网站一般多少钱贵州网站seo
  • 企业网站seo诊断请简述网络营销的特点
  • 蓝田县住房与城乡建设局网站网站推广软件下载
  • 做头像的日本网站有哪些网站友链交换平台
  • mvc做的网站seo代做
  • 江西人才招聘网官网网站优化的意义
  • 荣耀手机官方网站怎么建公司网站
  • 做食材的网站友情连接
  • 做网站的职业叫什么合肥网络公司seo