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


    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
                })
              }
            }
        });
      }
    })

  • 相关阅读:
    mybatis自学历程(二)
    mybatis自学历程(一)
    Hibernate入门教程(二):Hibernate核心API
    Hibernate入门教程(一):入门示例(Myeclipse)
    Python中反射的简单应用
    Struts2的Action访问
    Myeclipse中dtd代码提示
    B/+、索引原理
    postgres 模糊匹配
    jvm_第三章:垃圾收集与内存分配策略
  • 原文地址:https://www.cnblogs.com/taoshihan/p/11421426.html
Copyright © 2020-2023  润新知