小程序实现搜索框功能向后台发送请求

小程序实现搜索框功能向后台发送请求

我主要实现了小程序的功能,就没有后台代码了,直接看代码把。

.wxml

<input style="border:1px solid #ccc;float:left;" bindinput='changeSearch' placeholder="请输入名称搜索" type="text" name="name"></input>
<view bindtap="returns" class="inse">取消</view>
<!--取消返回上级 -->
<!-- 然后进行判断 -->
<!-- 查询出来自己自定义输出 -->
<view wx:if="{{inputValue != ''}}">
</view>
<!-- 没查询出来输出这个 -->
<view wx:if="{{inputValue == '' || inputValue == null}}">
</view>

.js

let api = require('../../utils/api.js');
let http = require('../../utils/httpUtil.js');
Page({
  data: {
  },
  //点击取消
  returns: function (e) {
    wx.navigateBack({
      delta: 1
    })
  },
  //搜索框
  changeSearch: function (e) {
    this.setData({
      inputValue: e.detail.value//监听键盘的输出
    })
    console.log(this.data.inputValue)
    let param = {
      "name": this.data.inputValue//把键盘的值传到后台去
    }
    let that = this
    let token = wx.getStorageSync('token');
    http.request(api.price, param, 'get', '查询', token, null).then(function (res) {
      that.setData({
        search: res.data//获取后台查询出来的值
      })
      console.log('返回数据:', res.data)
    })
  }
})

然后就是上面那些封装代码了

api.js

const baseUrl = '请求地址';

module.exports = {
  price: baseUrl +'文件地址'
}

httpUtil.js

function request(url, data, method, title, token, contentType) {
  wx.showNavigationBarLoading();
  return new Promise((resove, reject) => {
    wx.request({
      url: url,
      method: method.toUpperCase(),
      data: data,
      header: {
        'content-type': contentType ? contentType:'application/json',
        'Authorization': token
      },
      success: function (res) {
        wx.hideNavigationBarLoading();
        console.log(title + '返回的数据=======', res);
        if(res.code == 301){
          resove(null);
        }else{
          resove(res.data);
        }
      },
      fail: function (msg) {
        console.log(title + '请求失败=======', msg)
        wx.hideNavigationBarLoading();
        reject('fail')
      }
    })
  })
}
module.exports = {
  request: request,
}

分装的代码我前面几篇文章有的,有什么问题欢迎评论留言,我会及时回复你的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值