zoukankan      html  css  js  c++  java
  • 小程序请求后端接口实例

    小程序wxml页面button 按钮
    <view> <button bindtap="change">点击</button> //change 为js函数名 </view> 小程序js页面函数 Page({ change: function(event){ wx.request({ url: 'http://localhost:80/services/login/username', //如果不设置method 则默认get请求地址
        method: 'post',
    data: {       //可携带参数
            },
          header: {
            'content-type': 'application/json' // 默认值
          },
      
          success: function(res) {
            console.log(res.data)  //res.data  为接口返回值
          },
        })
      }
    
        
    })
    
    
    springboot后端接口:
        
    @RestController
    @RequestMapping("/login")
    public class loginController {
    
        @RequestMapping(value = "/username",method = RequestMethod.POST)
        public String login(HttpServletRequest request){
            System.out.println(request.getParameter("userName"));
            return "请求成功";
        }
    }


    //spring后端接口以及接收参数的方式
    @Component @Scope(
    "prototype") @Path("/medical") public class MedicalRecordAction { @POST @Path("/login") @Produces(value = MediaType.TEXT_PLAIN) public String login(@FormParam(value = "requestJson") String requestJson){ return medicalInsertService.login(requestJson); }
        
        //小程序前端请求方式以及请求参数       wx.request({ url: app.globalData.webServiceUrl
    +'/medical/login', method: 'post', data: { requestJson:"{'accountid':""+app.globalData.accountid+"",}", }, header: {'content-type': 'application/x-www-form-urlencoded' }, success:function(res){ if(res.data.code == 0){ for (let index in res.data.responseData) { //获取userid并配置全局userid app.globalData.userid = res.data.responseData[index].userid } }else{ wx.showToast({ title: res.data.responseData, icon: "none", }) } } })
    
    
    
     
     
  • 相关阅读:
    运用Unity实现依赖注入[结合简单三层实例]
    利用WCF与Android实现图片上传并传参
    如何修改被编译后DLL文件
    ASP.NET基础之HttpHandler学习
    ASP.NET基础之HttpContext学习
    ASP.NET基础之HttpModule学习
    WCF学习笔记之事务编程
    WCF学习笔记之传输安全
    WCF心跳判断服务端及客户端是否掉线并实现重连接
    CLR_Via_C#学习笔记之事件
  • 原文地址:https://www.cnblogs.com/yydxh/p/12692295.html
Copyright © 2011-2022 走看看