search
HomeWeChat AppletMini Program DevelopmentDetailed introduction of WeChat mini program request request

Talk about the data interaction of WeChat applet---wx.request

The official document clearly states that wx.request initiates an https request. If your server is an http site, you need to do configuration. You can refer to the article: http to https tutorial

But if you don’t have a server or don’t want to write background code, you can call the interface I provided here. This article will introduce how to use it.

First of all, we should start preparing from the server side. I use java. The framework is spring+springMVC+spring data.

The controller layer of the interface https://www.itit123.cn/itdragon/findAll

Source code:

@RequestMapping(value="findAll")
	@ResponseBody
	public Object listWxDatas(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@RequestParam(value = "pageSize", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sortType", defaultValue = "auto") String sortType, 
			ServletRequest request){
		pageSize = pageSize > 10? 10 : pageSize;
        try {
			Map<string> searchParams = Servlets.getParametersStartingWith(request, "search_");
			Page<wxdata> WxDatas = wxDataService.getWxData(searchParams, pageNumber, pageSize, sortType);
			List<map>> resultList = new ArrayList<map>>();
			for (WxData WxData : WxDatas) {
				Map<string> resultMap = new HashMap<string>();
				resultMap.put("id", WxData.getId());
				resultMap.put("title", WxData.getTitle());
				resultMap.put("content", WxData.getContent());
				resultMap.put("src", WxData.getImageUrl());
				resultMap.put("time", WxData.getCreatedDate());
				resultList.add(resultMap);
			}
			return gson.toJson(resultList);
        } catch (Exception e) {
			e.printStackTrace();
		}  
        return null;
	}</string></string></map></map></wxdata></string>

The general logic of the code is to check up to 10 pieces of data at a time, and Sort the results by id in descending order. The code does not put the entire object in a collection, but puts the required content (id, article title, pre-read content, main image, creation time) in a map, and then puts it in the collection and converts it into json format to return the data. (Note: This code is only for the current needs (query data). In the future, the code may be modified when doing pull-down refresh, pull-up loading, and search and sorting. Paging query: Getting Started with WeChat Mini Program 5: Swipe up to load and pull down to refresh).

After the server interface code is ready, you cannot rush to go online for testing. You can use postman of Google browser.

Detailed introduction of WeChat mini program request request

Check the printing results to ensure success

Detailed introduction of WeChat mini program request request

Then start preparing a test page on the WeChat applet Perform data interaction.

test.wxml:

<view>
    <textarea></textarea>
</view>
<button>request</button>

test.js:

Page({
  data: {
    textdata: "测试 wx.request",
  },
  RequestData: function () {
    var that = this;
    wx.request({
      url: 'https://www.itit123.cn/itdragon/findAll',
      data: {},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header 默认是application/json
      success: function (res) {
        // 操作json数据
        var text ="";
        for(var i in res.data) {
          text += i + "." + res.data[i].title + "\r\n";
        }
        that.setData({ textdata: text});
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})


Rendering of the test page:

Detailed introduction of WeChat mini program request request

If there is no problem in the test, then modify the code in list.js.

Can be modified according to your own needs. (I just changed func to ajax)

// pages/list/list.js
var app = getApp();
Page({
  data:{
    msgList:[]
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this
    app.ajax.req('/itdragon/findAll',{},function(res){  
      that.setData({
        msgList:res
      })
    });
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})


Because the returned data structure is exactly the format I need, I assigned it directly.

Rendering:

Detailed introduction of WeChat mini program request request

Learning points in this chapter:

1. Use of wx.request WeChat official documentation.

2. How to assign a value to a variable var that = this; that.setData({variable name: variable value}).

3. Development ideas.

The above is the detailed content of Detailed introduction of WeChat mini program request request. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools