fetch请求get方式以及post提交参数为formdata类型的数据

1.请求方式post,请求函数参数

_requestData(callback,_cityDt){
        
    const switchIp = "http://192.168.43.103/api/proxy";
    let formData = new FormData();
        formData.append("type","get");
        formData.append("open_redis",1);
        formData.append("redis_time",0);
    fetch(switchIp, {
            method:"POST",   
               body:formData
           }).then(
            (response)=>{
                if(response.ok){
                    return response.json()
                }else{
                    return Promise.reject({
                        status: response.status,
                        statusText: response.statusText
                    })
                }
            })
            .then((responseJsonData)=> {
                callback && callback(responseJsonData);
            }).catch((error)=> {
                console.log("getWatchHistory error " + error);
            });
    }
let _cityDt = “beijing”;  //需要传递的参数,不需要不传
this._requestData((data)=>{
    const res = JSON.parse(data);
    //执行数据的处理相关操作
    ........
    //执行初始化数据的操作    
    this._initData();
},_cityDt)

2.get请求

_requestData(callback,_id){    
     const url = `http://192.114.90.121/data/api/trend?evt=${_id}`;
     fetch(url, {}).then(
            (response)=>{
            if(response.ok){
                return response.json()
            }else{
                return Promise.reject({
                    status: response.status,
                    statusText: response.statusText
            })
         }
    }).then((responseJsonData)=> {
                callback && callback(responseJsonData);
            }).catch((error)=> {
                console.log("getWatchHistory error " + error);
            });
    }
let _id = "123";
this._requestData((data)=>{
  //执行数据渲染的函数
    this._play(data)
},_id)

猜你喜欢

转载自www.cnblogs.com/lmxHome/p/10750128.html
今日推荐