小程序踩坑异步请求json时,headers设置 "content-type": "application/x-www-form-urlencoded"...

本文详细介绍了微信小程序中使用wx.request进行网络请求的方法,包括设置URL、请求方式、请求头及数据格式等,并展示了如何处理请求成功和失败的情况。
wx.request({
url: url,
method:params.method,
data: params.data,
header: {
"content-type": "application/x-www-form-urlencoded"
},
success: function (res) {
console.log(res.data);
params.sCallback && params.sCallback(res.data)
},
fail:function(err){
this._processError(err);
}
})

转载于:https://www.cnblogs.com/caicaizi/p/8533133.html

PS C:\Users\Administrator> Invoke-CurlRequest -Url "https://httpbin.org/get" -OutputType Raw { "args": {}, "headers": { "Accept": "*/*", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Curl/8.15.0", "X-Amzn-Trace-Id": "Root=1-689f56f2-6362eb724074b2d13311b4ec" }, "origin": "112.40.123.16", "url": "https://httpbin.org/get" } PS C:\Users\Administrator> PS C:\Users\Administrator> Invoke-CurlRequest -Url "https://httpbin.org/get" -OutputType Object args headers ---- ------- @{Accept=*/*; Host=httpbin.org; User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) Curl/8.15.0; X-Amzn-Trace-... PS C:\Users\Administrator> PS C:\Users\Administrator> $body = @{ >> name = "John Doe" >> email = "john@example.com" >> } | ConvertTo-Json >> PS C:\Users\Administrator> Invoke-CurlRequest -Url "https://httpbin.org/post" -Method POST -Body $body -OutputType Object args : data : files : form : @{{ email: john@example.com, name: John=} headers : @{Accept=*/*; Content-Length=49; Content-Type=application/x-www-form-urlencoded; Host=httpbin.org; User-Agent =Mozilla/5.0 (Windows NT 10.0; Win64; x64) Curl/8.15.0; X-Amzn-Trace-Id=Root=1-689f56fb-3cf05f943ceea68929eee 7d9} json : origin : 112.40.123.16 url : https://httpbin.org/post PS C:\Users\Administrator> PS C:\Users\Administrator> $headers = @{ >> "Authorization" = "Bearer token123" >> "X-Custom-Header" = "Value" >> } >> PS C:\Users\Administrator> Invoke-CurlRequest -Url "https://httpbin.org/headers" -Headers $headers -OutputType Object headers ------- @{Accept=*/*; Authorization=Bearer token123; Host=httpbin.org; User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) ... PS C:\Users\Administrator> PS C:\Users\Administrator> # 重新加载模块 PS C:\Users\Administrator> Remove-Module CurlTools -ErrorAction SilentlyContinue PS C:\Users\Administrator> Import-Module CurlTools -Force -Verbose 详细信息: 正在从路径“E:\CurlTools\Modules\CurlTools\CurlTools.psd1”加载模块。 详细信息: 正在从路径“E:\CurlTools\Modules\CurlTools\CurlTools.psm1”加载模块。 详细信息: 正在导入函数“Get-CurlVersion”。 详细信息: 正在导入函数“Invoke-CurlRequest”。 PS C:\Users\Administrator> PS C:\Users\Administrator> # 测试原始输出 PS C:\Users\Administrator> Invoke-CurlRequest -Url "https://httpbin.org/user-agent" -OutputType Raw { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Curl/8.15.0" } PS C:\Users\Administrator> PS C:\Users\Administrator> # 测试对象输出 PS C:\Users\Administrator> Invoke-CurlRequest -Url "https://httpbin.org/user-agent" -OutputType Object | Format-Table user-agent ---------- Mozilla/5.0 (Windows NT 10.0; Win64; x64) Curl/8.15.0 PS C:\Users\Administrator> PS C:\Users\Administrator> # 测试复杂请求 PS C:\Users\Administrator> $response = Invoke-CurlRequest -Url "https://httpbin.org/post" -Method POST -Body '{"key":"value"}' -OutputType Object PS C:\Users\Administrator> $response.json PS C:\Users\Administrator> PS C:\Users\Administrator>
最新发布
08-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值