小程序获取HTTPS 网络请求

120 阅读1分钟

wx.request({

                      url: 'example.php',    //后台接口地址

                      method:'',             //默认为GET

                      data: {

                        x: '',               //后台需要的数据传参

                        y: ''

                      },

                      header: {

                        'content-type': 'application/json' // 请求头

                      },

                      success:(res)=> {

                        console.log(res.statusCode)   打印状态码 200请求成功

                      }

                    })



注册页面 html部分

<!--pages/register/register.wxml-->
<view class="flex row">
    <view class="label">昵称:</view>
    <input placeholder="请输入昵称" bindinput="nichenCtrl"/>
</view>
<view class="flex row">
    <view class="label">邮箱:</view>
    <input placeholder="请输入邮箱" bindinput="emailCtrl"/>
</view>
<view class="flex row">
    <view class="label">密码:</view>
    <input type="password" placeholder="请输入密码" bindinput="pwdCtrl"/>
</view>
<view class="flex row">
    <view class="label">确认密码:</view>
    <input type="password" placeholder="请确认密码" bindinput="repwdCtrl"/>
</view>
<view style="text-align: center;">
    <button plain type="primary" style="width:150px;" bindtap="registerCtrl">注册</button>
</view>

js部分

Page({

    /**
     * 页面的初始数据
     */
    data: {
        nichen: '',
        email: '',
        pwd: '',
        repwd: ''
    },
    nichenCtrl: function (e) {
        this.setValue(e, 'nichen')
    },
    emailCtrl: function (e) {
        this.setValue(e, 'email')
    },
    pwdCtrl: function (e) {
        this.setValue(e, 'pwd')
    },
    repwdCtrl: function (e) {
        this.setValue(e, 'repwd')
    },
    setValue: function (event, keyName) {
        let {
            detail: {
                value
            }
        } = event;
        /* keyName是变量所以要使用中括号 */
        this.setData({
            [keyName]: value
        })
    },

    registerCtrl: function () {
        if (!this.data.nichen || !this.data.email || !this.data.pwd || !this.data.repwd) {
            wx.showToast({
                title: "输入不能为空",
                icon: "error"
            })
            return
        }
        wx.request({
            method:'POST',
            url: 'https://api.shop.eduwork.cn/api/auth/register', 
            data: {
                name:this.data.nichen,
                email:this.data.email,
                password:this.data.pwd,
                password_confirmation:this.data.repwd
            },
            success:(res)=>{
                if(res.data&&res.data.status_code==422){
                    wx.showToast({
                        title: '注册失败',
                        icon:'error'
                    })
                }else{
                    wx.showToast({
                        title: '注册成功',
                        icon:'success'
                    })
                    setTimeout(()=>{
                        wx.navigateTo({
                          url: '/pages/login/login',
                        })
                    },1000)
                }
            }
        })
    },