微信小程式wx.request踩坑記錄
阿新 • • 發佈:2018-11-05
一:header['content-type']
為 application/json
的資料
前端
POST請求方式wx.request({ url: apiUrl, //替換成自己的伺服器地址 data: { aaa:1111 }, method:'POST|GET', header: { 'content-type': 'application/json'}, success:function(res){ callback(res); }, fail:function(){ console.log('介面呼叫失敗') } })
$a=file_get_contents('php://input'); echo $a; //{aaa:1111}GET請求方式
echo json_encode($_GET); //{aaa:1111}
二:header['content-type']
為 application/x-www-form-urlencoded
的資料
var param={aa:11,bb:22} var str = [] for (var p in param) { str.push(encodeURIComponent(p) + "=" + encodeURIComponent(param[p])) } str=str.join("&") console.log(str) //aa=11&bb=22 var apiUrl ='http://127.0.0.1' wx.request({ url: apiUrl, //替換成自己的伺服器地址 data: str|param, //POST用str,GET用param method:'POST|GET', header: { 'content-type': 'application/x-www-form-urlencoded'}, success:function(res){ callback(res); }, fail:function(){ console.log('介面呼叫失敗') } })
PHP服務端 POST獲取
echo json_encode($_POST); //{aa: "11", bb: "22"}GET獲取
echo json_encode($_GET); //{aa: "11", bb: "22"}