当前位置: 移动技术网 > IT编程>开发语言>PHP > [小程序]微信小程序获取input并发送网络请求

[小程序]微信小程序获取input并发送网络请求

2019年08月28日  | 移动技术网IT编程  | 我要评论

机械加工设备网,我想和你唱第二季嘉宾,中央十套

1. 获取输入框数据
wxml中的input上增加bindinput属性,和方法值
在js部分定义与之对应的方法,只要在输入的时候,数据就会绑定调用到该方法,存入data属性变量中

2. 调用get请求发起网络请求
调用wx.request发起网络请求

3.调用微信toast接口展示结果

4.按钮绑定bindtap属性,当按钮点击的时候会调用对应的方法

index.wxml部分

<view class="indexinput">
  <input  maxlength="100" bindinput="getemail" placeholder="邮箱地址" />
</view>
<view class="indexinput">
  <input password  maxlength="30" bindinput="getpasswd" placeholder="密码" />
</view>
<view class="indexbutton">
<button type="primary" bindtap="checklogin" loading="{{loading}}"> 登录 </button>
</view>

index.js部分

//index.js
//获取应用实例
const app = getapp()

page({
  data: {
    email:"",
    passwd:"",
  },

  onload: function () {

  },
  //获取输入框数据
  getemail:function(e){
    this.setdata({
      email: e.detail.value
    })
  },
  //获取输入框数据
  getpasswd: function (e) {
    this.setdata({
      passwd: e.detail.value
    })
  },
  /*
  * 验证用户名密码是否正确
   */
  checklogin:function(){
    var email=this.data.email;
    var passwd = this.data.passwd;
    var data={
      loginfrom:"app",
      email: email,
      psw: passwd,
      output: "json"
    };
    var url = "https://api.sopans.com/third/login.php";
    wx.request({
        url: url,
        method: 'get',
        data: data,
        header: {
          'content-type': 'application/json'
        },
        success(res) {
          if(res.data.code=200){
            wx.showtoast({
              title: '成功',
              icon: 'success',
              duration: 2000
            })
          }
        }
    });
  }
})

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网