通用的一些流程
要判断http返回码
要判断body里面业务返回码
是否能够跨域
是否能够携带Cookie

常用的方法有fetch, ajax, axios, XMLHttpRequest,request,下面有具体介绍

  1. fetch
    https://scotch.io/tutorials/how-to-use-the-javascript-fetch-api-to-get-data
fetch(url) // Call the fetch function passing the url of the API as a parameter
.then(function() {// Your code for handling the data you get from the API
})
.catch(function() {// This is where you run code if the server returns any errors
});
  1. axios
axios.post('/user', {firstName: 'Fred',lastName: 'Flintstone'}).then(function (response) {console.log(response);}).catch(function (error) {console.log(error);});
  1. ajax
 $.ajax({type: "GET",url: "test.json",data: {username:$("#username").val(), content:$("#content").val()},dataType: "json",success: function(data){console.log(data)}});
  1. XMLHttpRequest
    细节使用 https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest
let xhr = new XMLHttpRequest();
xhr.open('GET', '/url', true);
xhr.send();
  1. request
const request = require('request');
request('http://www.google.com', function (error, response, body) {console.error('error:', error); // Print the error if one occurredconsole.log('statusCode:', response && response.statusCode); // Print the response status code if a response was receivedconsole.log('body:', body); // Print the HTML for the Google homepage.
});

一种比较可行的方案是在utils里面把请求处理到json层次,然后业务层次只要判读json里面的业务码就好了。
因为一个项目,origin固定,path传参数,post方法可以固定,header头可以可固定,body传参数

问题:
Cross-Origin Read Blocking (CORB) blocked cross-origin response

前端使用js发起http请求的几种方法相关推荐

  1. 模拟发起http请求的几种方法

    如何使用http协议调用服务器端的服务. 在日常工作中很多时候我们会需要测试自己提供出来的方法, 很多时候一些方法跨平台提供给第三方使用,一般与服务器通信协议都选择http协议, 建议可以去仔细学习一 ...

  2. Vue3(撩课学院)笔记09-axios简介,发起get请求的两种方式,发起带参的get及post请求,发起并发请求,并发请求结果将数组展开,axios全局配置,axios配置及封装,请求和响应拦截

    1.axios简介 axios是基于promise可以用于浏览器和node.js的网络请求库,在服务器端使用原生node.js,在浏览气短使用ajax(即XMLHttpRequests) 2.axio ...

  3. iOS: JS和Native交互的两种方法,iosjsnative交互

    iOS: JS和Native交互的两种方法,iosjsnative交互 背景: UIWebView: iOS 用来展示 web 端内容的控件. 1. 核心方法: - (NSString*)string ...

  4. 【javascript基础——系列10】js中隐藏元素的几种方法以及代码

    系列文章 [javascript基础--系列1]前端页面ajax连接后台服务器传输数据 [javascript基础--系列2]前端页面axios连接后台服务器传输数据 [javascript基础--系 ...

  5. http 请求的7 种方法

    http 请求的7 种方法 1.get 2.post 3.put 4.delete 5.head 6.trace 7.opinions head 请求和 get请求类似,但是服务器只响应首部,不会返回 ...

  6. js中当等于最小值是让代码不执行_网页中JS函数自动执行常用三种方法

    本文为大家分享了在网页中JS函数自动执行常用方法,供大家参考,具体内容如下 一.JS方法 1.最简单的调用方式,直接写到html的body标签里面: 2.在JS语句调用: function myfun ...

  7. php发送post请求的三种方法

    这篇文章主要介绍了php发送post请求的三种方法,分别使用curl.file_get_content.fsocket来实现post提交数据,需要的朋友可以参考下 class Request{ pub ...

  8. js实现阶乘算法的三种方法

    js实现阶乘算法的三种方法 // 非递归写法 function f(n) {if (0 === n) {return 1;}let res = 1;for (let i = 1; i <= n; ...

  9. js刷新页面有哪几种方法

    js刷新页面有哪几种方法 一.总结 一句话总结:location属性的reload方法即可:document.location.reload() 1.页面刷新有哪常见的8种方法? 1,history. ...

最新文章

  1. 御剑情缘服务器维护,御剑情缘10月31日安卓区部分服务器数据互通公告 10.31合服名称与时间[图]...
  2. 博客园计划增加的功能
  3. fifo 上使用 select -- 转
  4. 第二冲刺阶段绩效评估
  5. oracle数据库王珊,数据库系统概论 王珊 教材部分总结
  6. GLCM opencv
  7. wget下载命令使用说明
  8. 最新android APP框架介绍
  9. 大众点评产品分析报告
  10. 算法学习:LeetCode-592. 分数加减运算
  11. 【软件测试基础】软件测试模式
  12. Spark Core实战:解析Tomcat日志
  13. Oracle11g 体系结构
  14. html5 blockquote,html5 – 正确的HTML Blockquote
  15. 【防诈骗】QQ账号安全检测工具-腾讯出品
  16. click me? no
  17. 手机验证码接收系统如何选择
  18. 文件下载(解决中文文件名下载乱码问题)(二)
  19. 熬之滴水穿石:Servlet--Web万物之源(1)
  20. jdk版本和maven版本的匹配问题,maven报错method ‘void <init>()‘ not found

热门文章

  1. 【实习生笔试面试】腾讯2013实习生电话面试总结
  2. 基本数据类型的分类 0125
  3. sqlserver增删改格式整理 1123
  4. 对象数组快速掌拨 java 0913
  5. linux-关于用户的约定
  6. js中的字符长度问题
  7. 全新Docker Hub发布:提供查找、存储和共享容器镜像单一体验
  8. 深入理解Redis系列之SpringBoot集成Redis
  9. 移动端开发touchstart,touchmove,touchend事件详解和项目
  10. JavaScript高级程序设计(第三版)学习笔记22、24、25章