微信小程序---发送网络请求

1. 微信开发者工具设置(跨域设置)

 

 

 2.html

<view class="code">
  <input type="text" bindinput="bindCode" placeholder="输入验证码" />
    <text bindtap="get_code">点我获取验证码</text>
  </view>

3. js

data: {
    phone: "",
    code: "",
  },

// 双向绑定验证码
  bindCode: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  // 获取验证码
  get_code: function () {
    var phone = this.data.phone
    // 前端校验1 手机号长度是否为11位
    if (phone.length != 11) {
      console.log(phone.length)
      wx.showToast({
        title: '手机号长度不对',
        icon: 'error',
        duration: 1500
      })
      return
    }
    // 前端校验2 手机号是否符合格式
    var rag = /^1[3-9][0-9]{9}$/
    if (!rag.test(phone)) {
      wx.showToast({
        title: '手机号格式不对',
        icon: 'error',
        duration: 1500
      })
      return
    }
    wx.request({
      url: 'http://127.0.0.1:8030/api/sms/code/',
      method: "POST",
      data: {
        phone: phone,
        tpl: "login",
      },
      success: (res) => {
        if (res.data.status === 200) {
          wx.showToast({
            title: '验证码已发送',
            icon: 'success',
            duration: 1500
          })
          return
        } else {
          wx.showToast({
            title: '请求失败',
            icon: 'error',
            duration: 1500
          })
          return
        }
      },
    })
  },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑台风走

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值