1,POST帶參數)fetch提交json格式的數據到服務器:
1 //fetch替換vue-resource 2 let jsonData= { 3 params1:'param1_value', 4 params2:'param2_value' 5 }; 6 fetch( 7 url(地址), 8 { 9 method: 'POST', 10 credentials: 'include', 11 headers: {(添加頭文件) 12 'Content-Type': 'application/json;charset=UTF-8'(指定數據類型和編碼), 13 'Authorization': 'Bearer ' + localStorage.access_token(在請求信息中添加assess_token驗證碼), 14 }, 15 body: JSON.stringify(jsonData), 16 } 17 ).then(function(res){ 18 return res.json().then((data)=>{(返回的res為原生Promise對象,需要轉換) 19 console.log(data) 20 }) 21 }); 22 //end fetch替換vue-resource
2,GET帶參數)fetch提交json格式的數據到服務器:
1 params1='param1_value' ;
2 params2='param2_value' ; 3 4 let url = 'http://www.cnblogs.com'+'?param1='+param1_value+'¶m2='+param2_value; (get方式只能把參數拼接到url地址中進行傳遞) 5 6 fetch( 7 url(地址), 8 { 9 method: 'GET', 10 credentials: 'include', 11 headers: {(添加頭文件) 12 'Content-Type': 'application/json;charset=UTF-8'(指定數據類型和編碼), 13 'Authorization': 'Bearer ' + localStorage.access_token(在請求信息中添加assess_token驗證碼), 14 }, 15 16 } 17 ).then(function(res){ 18 return res.json().then((data)=>{(返回的res為原生Promise對象,需要轉換) 19 console.log(data) 20 }) 21 }); 22 //end fetch替換vue-resource
