Javascript拼接路径

  • 时间:
  • 浏览:
  • 来源:互联网
function errorIfMissing(){
        throw new Error("缺少参数")
    }
    function request(url = errorIfMissing,{ method ="GET",data={}}={}){
        if(method === "GET"){
            let params =""
            let index = 0
            for(let key in data){
                let value = data[key]
                params += `${(index > 0) ? "&" : ""}${key}=${value}`
                index++
            }
            if(params !== ""){
                url+= "?"+params
            }
            console.log(params);
        }
        console.log(url)
    }
    // request("api",{data:{id:3,page:2}})
    request("http://www.example.com/api",{method: "GET",data:{id:3}})

以上就是Javascipt拼接路径的全部代码了

本文链接http://metronic.net.cn/metronic/show-22213.html