get请求

let url="http://127.0.0.1:8090/data2";
fetch(url).then(function(data){
//data不是服务器的数据 是respose对象
//data.text() 是promise
return data.text();
}).then(function(res){
console.log(res);
})

get请求传值

//Get请求传值
let url2="http://127.0.0.1:8090/data3?id=1";
fetch(url2,{method:'get'}).then(function(data){
//data不是服务器的数据 是respose对象
//data.text() 是promise
return data.text();
}).then(function(res){
console.log(res);
})

post请求传值

//post请求传值
let url3="http://127.0.0.1:8090/data4";
fetch(url3,{
method:'post',
body:'user=lisi&pwd=123',
headers:{'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function(data){
//data不是服务器的数据 是respose对象
//data.text() 是promise
return data.text();
}).then(function(res){
console.log(res);
})

delete请求传值

//delete 请求传值
let url4="http://127.0.0.1:8090/data5/1901";
fetch(url4,{
method:'delete',
}).then(function(data){
//data不是服务器的数据 是respose对象
//data.text() 是promise
return data.text();
}).then(function(res){
console.log(res);
})

put请求传值

//put请求 发送JSON
var cf={user:"lisi",sex:"男"}
let url5="http://127.0.0.1:8090/data6";
fetch(url5,{
method:'put',
body:JSON.stringify(cf),
headers:{'Content-Type':'application/json'}
}).then(function(data){
//data不是服务器的数据 是respose对象
//data.text() 是promise
return data.text();
}).then(function(res){
console.log(res);
})

请求返回JSON

//请求返回JSON
let url6="http://127.0.0.1:8090/json";
fetch(url6).then(function(da){
return da.json();
}).then(function (res){
//上面是JSON的话就可以直接取 不用去转换
console.log(res);
//字符串转JSON
/*let cf= JSON.parse(res);
console.log(cf.age);*/
})