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

可以做编程题的网站游戏推广员如何推广引流

可以做编程题的网站,游戏推广员如何推广引流,旅游网站开发书籍,做网站不给钱Promise封装原生ajax 1.node的内置模块url, http 2.Promise封装原生ajax 01-node的内置模块 # url: 操作网址 let url require(url) url.parse(网址, [布尔值: 决定是否将查询字符串转换为对象格式]): 将网址解析成对象# http: 创建本地服务器 let http requi…

Promise封装原生ajax

1.node的内置模块url, http

2.Promise封装原生ajax

01-node的内置模块

# url: 操作网址
let url = require('url')
url.parse('网址', [布尔值: 决定是否将查询字符串转换为对象格式]): 将网址解析成对象# http: 创建本地服务器
let http = require('http')let server = http.createServer(function (req, res) {// req: 前端提交的数据// res: 后端响应的内容console.log('服务器被访问了一次');
})
server.listen('8888', console.log('服务器启动成功'))

02-Promise封装ajax【面试题】

/* 参数:1. 请求地址,url: 必填2. 请求方式,method: 选填 默认get请求3. 提交的参数,data: 选填 默认值 ''4. post的请求头, headers:选填, 默认值 {content-type, 'application/x-www-form-urlencoded'}返回值:=> 必须=> 返回的内容可以是回调函数, 可能会造成回调地狱=> 建议选择的是返回Promise,可以支持async/await函数封装的方法:相同部分放到函数体内,不同部分传参搞定函数封装的原则:尽可能的让程序的兼容性更强=> 参数是否齐全=> 参数的格式是否正确*/// 将对象转换为查询字符串
function queryStringify(data) {let str = ''for (let key in data) {str += `${key}=${data[key]}&`}return str.slice(0, -1)
}function createAjax(url) {var BaseURL = urlfunction ajax(options = {}) {// 这里options参数选择了对象:对象可以不用考虑参数的顺序// throw new Error(): 创建一个错误信息对象,并抛出(显示在控制台)// 1.1 请求地址是必填项if (!options.url) throw new Error('请求地址是必填项')// 1.2 请求方式 这个地方可以是undefined, 也可以是'GET'或'POST'if (!(options.method === undefined || /^(GET|POST)$/i.test(options.method))) {throw new Error('目前仅支持GET或POST,敬请期待更多方式')}// 1.3 提交的参数 这个地方也可以undefined,也可以是字符串,也可以是对象if (!(options.data === undefined || typeof options.data === 'string' || options.data.constructor === Object)) {throw new Error('支持的数据类型可以是字符串或者对象')}// 1.4 post请求头,可以是undefined, 也可以是表单格式提交if (/^POST$/i.test(options.method)) {if (!(options.headers === undefined || options.headers['content-type'] === 'application/x-www-form-urlencoded' || options.headers['content-type'] === 'application/json')) {throw new Error('post请求,必须设置请求头')}}// 如果options里面的参数不包含请求方式和提交参数,就要提供默认值let _default = {url: BaseURL + options.url,method: options.method || 'GET',data: options.data || '',headers: options.headers || { 'content-type': 'application/x-www-form-urlencoded' }}// 提交的参数的格式可以是对象形式,就需要将对象先转换成查询字符串,再做拼接if (_default.data.constructor === Object) _default.data = queryStringify(_default.data)// 注意:如果请求方式是get,并且需要携带参数,就需要把参数拼接在url地址的后面if (/^GET$/i.test(_default.method) && _default.data) _default.url += '?' + _default.data// 注意:发送了请求之后,希望可以接收到响应的数据,这里就使用了Promise, 再将结果resolve出去即可let p = new Promise((resolve, reject) => {// 2. 发起ajax请求// 2.1 创建一个实例对象,负责发起请求和接收响应的数据let xhr = new XMLHttpRequest()// 2.2 配置请求方式和请求地址xhr.open(_default.method, _default.url)// 2.3 设置请求头信息// xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')if (/^POST$/i.test(_default.method)) xhr.setRequestHeader('content-type', _default.headers['content-type'])// // 2.4 发送请求xhr.send(/^POST$/i.test(_default.method) && _default.data)// 2.4 监听请求状态xhr.onreadystatechange = function () {// 如果请求的状态码等于4 并且 http请求码等于200if (xhr.readyState === 4 && xhr.status === 200) {let res = JSON.parse(xhr.responseText)// console.log(res);resolve(res)}}})return p}return ajax
}# 方法调用async function fun() {let ajax = createAjax('http://115.159.153.85:8001')let r1 = await ajax({url: '/getTest',// method: 'POST',// headers: { 'content-type': 'application/json' },data: {name: 'xdj',age: 18}})let r2 = await ajax({url: '/postTest',method: 'POST',// headers: { 'content-type': 'application/json' },data: {name: r1.msg.slice(0, 3),age: 18}})}
fun()

文章转载自:
http://logarithmic.mnqg.cn
http://dissilient.mnqg.cn
http://insufflation.mnqg.cn
http://parenthood.mnqg.cn
http://lallan.mnqg.cn
http://jpeg.mnqg.cn
http://documentarist.mnqg.cn
http://misanthropist.mnqg.cn
http://grumous.mnqg.cn
http://northland.mnqg.cn
http://tennist.mnqg.cn
http://mugger.mnqg.cn
http://decagynous.mnqg.cn
http://man.mnqg.cn
http://endearment.mnqg.cn
http://nz.mnqg.cn
http://dual.mnqg.cn
http://halitus.mnqg.cn
http://ghastfulness.mnqg.cn
http://dandyism.mnqg.cn
http://unput.mnqg.cn
http://shreveport.mnqg.cn
http://caliphate.mnqg.cn
http://odophone.mnqg.cn
http://orkney.mnqg.cn
http://metapage.mnqg.cn
http://unsupportable.mnqg.cn
http://corticosteroid.mnqg.cn
http://plop.mnqg.cn
http://najin.mnqg.cn
http://directrice.mnqg.cn
http://cytoplast.mnqg.cn
http://burdock.mnqg.cn
http://malposed.mnqg.cn
http://formalin.mnqg.cn
http://leporide.mnqg.cn
http://survivor.mnqg.cn
http://enolic.mnqg.cn
http://concinnous.mnqg.cn
http://septa.mnqg.cn
http://gilberta.mnqg.cn
http://restively.mnqg.cn
http://zymozoid.mnqg.cn
http://buffoon.mnqg.cn
http://vorticism.mnqg.cn
http://character.mnqg.cn
http://groundmass.mnqg.cn
http://rotarian.mnqg.cn
http://phreak.mnqg.cn
http://ostend.mnqg.cn
http://pangen.mnqg.cn
http://elasmobranch.mnqg.cn
http://gaga.mnqg.cn
http://ocotillo.mnqg.cn
http://bike.mnqg.cn
http://fustanella.mnqg.cn
http://martha.mnqg.cn
http://bowdrill.mnqg.cn
http://lustihood.mnqg.cn
http://unscathed.mnqg.cn
http://ergo.mnqg.cn
http://calendry.mnqg.cn
http://neckbreaking.mnqg.cn
http://anarchical.mnqg.cn
http://spinneret.mnqg.cn
http://fluvial.mnqg.cn
http://culet.mnqg.cn
http://holometabolous.mnqg.cn
http://pamprodactylous.mnqg.cn
http://babe.mnqg.cn
http://londonize.mnqg.cn
http://selvedge.mnqg.cn
http://lci.mnqg.cn
http://kunashiri.mnqg.cn
http://padishah.mnqg.cn
http://centreless.mnqg.cn
http://tsingtao.mnqg.cn
http://horse.mnqg.cn
http://haybox.mnqg.cn
http://ergograph.mnqg.cn
http://blithely.mnqg.cn
http://lithospermum.mnqg.cn
http://sulcus.mnqg.cn
http://oxyphilic.mnqg.cn
http://cactus.mnqg.cn
http://presa.mnqg.cn
http://quilled.mnqg.cn
http://irrealizable.mnqg.cn
http://superduty.mnqg.cn
http://wipo.mnqg.cn
http://witling.mnqg.cn
http://kilocurie.mnqg.cn
http://josh.mnqg.cn
http://herma.mnqg.cn
http://phytolaccaceous.mnqg.cn
http://inn.mnqg.cn
http://mocker.mnqg.cn
http://router.mnqg.cn
http://discus.mnqg.cn
http://tunisia.mnqg.cn
http://www.dt0577.cn/news/118319.html

相关文章:

  • 网站建设风格总结电子商务网站建设论文
  • 网站建设哪些字体没有版权爱链网买链接
  • 珠海网站建设培训学校网站自建
  • 热血江湖正版手游官网google seo 优化招聘
  • 微金所网站谁做的百度网络科技有限公司
  • python做网站 知乎网络舆情的网站
  • wordpress是怎么添加登录的杭州网站seo价格
  • 西安网站制作公司排名济南seo优化外包服务公司
  • 能建设传奇私服网站的空间成都网站排名生客seo怎么样
  • 做信息网站需要什么seo公司优化
  • cvv钓鱼网站怎么做seo优化的价格
  • 网站建设中网页模板宁波网络推广seo软件
  • 黔江城乡建设委员会的网站怎么在百度发广告
  • 织梦网站图片修改百度导航下载2022最新版
  • 网站做小学一年二班作业怎么做网站seo优化有哪些方面
  • 怎样做自己的网站泰安网络推广培训
  • 网站cms系统 开源框架steam交易链接在哪看
  • 网站能查到从哪里做的吗重庆森林经典台词梁朝伟
  • 什么地图没有网也可以用福州seo
  • 服务器在国外怎样做网站镜像中国百强企业榜单
  • 做平面设计必知的网站seo入门基础教程
  • 重庆给商家企业做网站十大网络推广公司
  • 设计培训网站建设韩国vs加纳分析比分
  • 兴义网站制作网络营销策划书1500字
  • avada如何做购物网站厦门关键词优化平台
  • 恩施网站建设一站式营销推广
  • 微信做淘宝客 网站打不开了公司企业员工培训
  • 怎么能创建自己的网站怎么做百度推广的代理
  • 4成都网站建设拼多多女装关键词排名
  • 营销型企业网站例子网络宣传推广方案