通用的一些流程
要判斷http返回碼
要判斷body里面業務返回碼
是否能夠跨域
是否能夠攜帶Cookie
常用的方法有fetch, ajax, axios, XMLHttpRequest,request,下面有具體介紹

- 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
});
- axios
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
- ajax
$.ajax({
type: "GET",
url: "test.json",
data: {username:$("#username").val(), content:$("#content").val()},
dataType: "json",
success: function(data){
console.log(data)
}
});
- XMLHttpRequest
細節使用 https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest
let xhr = new XMLHttpRequest();
xhr.open('GET', '/url', true);
xhr.send();
- request
const request = require('request');
request('http://www.google.com', function (error, response, body) {
console.error('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.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
