亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

微信小程序wx.request使用POST請(qǐng)求時(shí)后端無(wú)法獲取數(shù)據(jù)解決辦法

 更新時(shí)間:2021年03月07日 09:28:57   作者:祝君圓夢(mèng)  
這篇文章主要介紹了微信小程序wx.request使用POST請(qǐng)求時(shí)后端無(wú)法獲取數(shù)據(jù)解決辦法,解決辦法其實(shí)也很簡(jiǎn)單,有需要的同學(xué)可以嘗試下

遇到的坑:

例如在寫微信小程序接口時(shí),method請(qǐng)求方式有POST和GET兩種,為了數(shù)據(jù)安全,我們會(huì)偏向于使用POST請(qǐng)求方式訪問(wèn)服務(wù)器端;

當(dāng)我們使用POST方式請(qǐng)求時(shí),后端無(wú)法獲取到傳送的參數(shù),但使用GET方式卻是可以的。

解決辦法:

設(shè)置請(qǐng)求的 header頭:

header: { "Content-Type": "application/x-www-form-urlencoded" },

特別注意:post請(qǐng)求必須寫method: 'POST',因?yàn)閣x.request默認(rèn)是GET請(qǐng)求的。

示例代碼:

微信小程序的 index.js

wx.request({ 
 url: 'https://后端網(wǎng)址/user/updatePhone.html',
 method: 'POST',
 data: { phone: _phone, openid: _openid},
 header: { "Content-Type": "application/x-www-form-urlencoded" },
 success: res => {
 console.log(res.data);
 }
});

thinkphp后端控制器代碼:

<?php
namespace app\car\controller;
use think\Controller;
use think\Db;
use think\Request;
 
class User extends Base
{
	public function _initialize(){		
		parent::_initialize();
	} 
 
 public function updatePhone(){
 if(!isset($_POST['phone'])||!isset($_POST['openid'])){
  header("Content-type: text/html; charset=utf-8"); 
  echo '參數(shù)錯(cuò)誤'.$_POST['phone'];
  exit;
 }	
		$openid= trim($_POST['openid']);
		try{
			$updata['tel'] = trim($_POST['phone']);
			Db::name('user')->where('wxopenid',$openid)->update($updata);
			$code=1;
			$msg="修改成功";
		} catch (\Exception $e) {
			$code=0;
			$msg="修改失敗";
		}
		return $this->outputMsg($code,$msg);
 }
}

到此這篇關(guān)于微信小程序wx.request使用POST請(qǐng)求時(shí)后端無(wú)法獲取數(shù)據(jù)解決辦法的文章就介紹到這了,更多相關(guān)微信小程序使用POST請(qǐng)求時(shí)后端無(wú)法獲取數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論