小程序发起 HTTPS 网络请求

153 阅读1分钟

小程序发起 HTTPS 网络请求

                    wx.request({

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

                      method:'',             //默认为GET

                      data: {

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

                        y: ''

                      },

                      header: {

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

                      },

                      success:(res)=> {

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

                      }

                    })

简易登录界面传参 登录后台获得数据

       

        

         data: {eml:'',pwd:''},

              ipt1:function(e){

                this.setData({                   //通过bindinput方法获取input内的值

                 eml:e.detail.value

               })

         },

         在上面代码里把获取到值进行传参

         data:{

              email:this.data.eml,                //传入获取到的值值

              password:this.data.pwd

         }

         success:(res)=>{

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

         }

简易注册界面

<view class="flex row">

    <view class="label">昵称:

    <input placeholder="请输入昵称" bindinput="nichenCtrl"/>

<view class="flex row">

    <view class="label">邮箱:

    <input placeholder="请输入邮箱" bindinput="emailCtrl"/>

<view class="flex row">

    <view class="label">密码:

    <input placeholder="请输入密码" bindinput="pwdCtrl"/>

<view class="flex row">

    <view class="label">确认密码:

    <input placeholder="请确认密码" bindinput="repwdCtrl"/>

<view style="text-align: center;">

    <button plain type="primary" style="width:150px;" bindtap="registerCtrl">注册

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: 'api.shop.eduwork.cn/api/auth/re…',

          data: {

              name: this.data.nichen,

              email: this.data.email,

              password: this.data.pwd,

              password_confirmation: this.data.repwd

          },

          success(res) {

              console.log(res.data)

              if (res.data&&res.data.status_code == 422) {

                  wx.showToast({

                      title: "注册失败",

                      icon: "error"

                  })

              } else {

                  wx.showToast({

                      title: "注册成功",

                      icon: "success"

                  })

                  setTimeout(()=>{

                      wx.navigateTo({

                        url: '/pages/index/index',

                      })

                  },1000)

              }

          }

      })

  }