| 一直在搜索各种资料,但是都没有特别全面的,最终七拼八凑,解决了这个问题前端代码wxml <view class="container">
  <button bindtap="senCode">发送code</button>
</view>
 js 
const app = getApp()
Page({
  data: {},
  senCode(){
     
     wx.login({
      success: function(res) {
        if (res.code) {  
          console.log(res.code);
          
          wx.request({
            url: '后端api地址',
            method:'POST',
            
            data: {
              code: res.code    
            },
            header: { 
                   "Content-Type": "application/x-www-form-urlencoded" 
          },
          })
        } else {
          console.log('获取用户登录态失败!' + res.errMsg)
        }
      }
    });
  },
  onLoad() {}
})
 注意 --一开始数据一直发送不到后端,各种搜索添加了method,指定为post,然后添加 header: { 
                   "Content-Type": "application/x-www-form-urlencoded" 
          },
 后端代码–只有一个controllerpackage com.example.demo.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
@Controller
public class SendController {
    @ResponseBody
    @RequestMapping(value = "/send",method = POST)
    public static void getOpenid(@RequestParam(value="code",required=false)String code){
        
        String WX_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
        
        String requestUrl = WX_URL.replace("APPID", "").
                replace("SECRET", "").replace("JSCODE", code).
                replace("authorization_code", "authorization_code");
        
        JSONObject convertvalue=new JSONObject();
        
        String  returnvalue=GET(requestUrl);
        
        convertvalue= (JSONObject) JSON.parse(returnvalue);
        String session_key= (String) convertvalue.get("session_key");
        String openid= (String) convertvalue.get("openid");
        System.out.println(returnvalue);
        System.out.println("openid:"+openid);
        System.out.println("session_key:"+session_key);
    }
    
    public static String GET(String url) {
        String result="";
        BufferedReader in = null;
        InputStream is = null;
        InputStreamReader isr = null;
        try{
            
            URL realUrl = new URL(url);
            
            URLConnection conn = realUrl.openConnection();
            conn.connect();
            is = conn.getInputStream();
            isr = new InputStreamReader(is);
            in = new BufferedReader(isr);
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        }catch (Exception e){
          e.printStackTrace();
            System.out.println("异常出现");
        }
        
        finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (is != null) {
                    is.close();
                }
                if (isr != null) {
                    isr.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("出现异常");
            }
        }
        return result;
    }
}
 注意–如果要通过postman进行测试,注意传的数据格式 否则会数据插入失败。
 经过以上步骤即可成功向后端传入code.方法,注意请求的方法不同,header里的内容也不一样header{
'Content-Type': 'application/json' 
},
header: { 
"Content-Type": "application/x-www-form-urlencoded" 
}
 |