小程序與后端Java接口交互實現(xiàn)HelloWorld入門
第一步:后端簡單建個SpringBoot項目,提供一個 helloWorld接口;
版本選用 2.2.6.RELEASE
package com.java1234.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author java1234_小鋒 * @site www.java1234.com * @company 南通小鋒網(wǎng)絡科技有限公司 * @create 2021-07-04 17:43 */ @RestController public class HelloWorldController { @GetMapping("/helloWorld") public String helloWorld(Integer id){ return "helloWorld "+id; } }
application.yml
server: port: 80 servlet: context-path: / tomcat: uri-encoding: utf-8
瀏覽器訪問:http://localhost/helloWorld?id=1
頁面顯示:
helloWorld 1
第二步:新建一個helloWorld 微信小程序,請求后端
helloWorld.js
通過微信小程序API wx.request調用后端接口
// pages/helloWorld.js Page({ /** * 頁面的初始數(shù)據(jù) */ data: { result:"請求后臺中..." }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function (options) { var that=this; this.getData(that); }, getData(that){ wx.request({ url: 'http://localhost/helloWorld', method:"GET", data:{ id:100 }, header: { 'content-type': 'application/json' // 默認值 }, success(res){ console.log(res.data); console.log(that) that.setData({ result:res.data }) } }) }, /** * 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成 */ onReady: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面顯示 */ onShow: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面隱藏 */ onHide: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面卸載 */ onUnload: function () { }, /** * 頁面相關事件處理函數(shù)--監(jiān)聽用戶下拉動作 */ onPullDownRefresh: function () { }, /** * 頁面上拉觸底事件的處理函數(shù) */ onReachBottom: function () { }, /** * 用戶點擊右上角分享 */ onShareAppMessage: function () { } })
helloWorld.wxml
<!--pages/helloWorld.wxml--> <text>返回值:{{result}}</text>
運行報錯了:
VM8 asdebug.js:1 Cannot send network request to localhost.(env: Windows,mp,1.05.2105170; lib: 2.18.0)
這里我們需要設置下:
詳情->本地設置->勾選 “不校驗合法域名、web-view (業(yè)務域名)、TLS版本以及HITPS證書”
勾選后,重新編譯,運行OK;
擴展下,如果是域名調用,比如 http://localhost 改成 http://www.java1234.com
報錯:
如若已在管理后臺更新域名配置,請刷新項目配置后重新編譯項目,操作路徑:“詳情-域名信息”
VM8 asdebug.js:1 http://www.java1234.com 不在以下 request 合法域名列表中,請參考文檔:https://developers.weixin.qq.com/miniprogram/dev/framework/ability/network.html(env: Windows,mp,1.05.2105170; lib: 2.18.0)
我們打開 https://developers.weixin.qq.com/miniprogram/dev/framework/ability/network.html
微信小程序對于域名調用會有一些限制,還需要配置,比如僅支持https,•域名不能使用 IP 地址(小程序的局域網(wǎng) IP 除外)或 localhost;
服務器域名請在 「小程序后臺-開發(fā)-開發(fā)設置-服務器域名」 中進行配置:
到此這篇關于小程序與后端Java接口交互實現(xiàn)HelloWorld入門 的文章就介紹到這了,更多相關小程序與后端Java接口交互內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Boot REST國際化的實現(xiàn)代碼
本文我們將討論如何在現(xiàn)有的Spring Boot項目中添加國際化。只需幾個簡單的步驟即可實現(xiàn)Spring Boot應用的國際化,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10springboot?ElasticSearch如何配置自定義轉換器ElasticsearchCustomConver
這篇文章主要介紹了springboot?ElasticSearch如何配置自定義轉換器ElasticsearchCustomConversions問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08java Socket編程實現(xiàn)I/O多路復用的示例
本文主要介紹了java Socket編程實現(xiàn)I/O多路復用的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-09-09