前端给后端传数据的几种方式

05-09 阅读 0评论

前端给后端传数据的几种方式

  • 1.发送get请求将参数通过?拼接在url后面
  • 2.将参数拼接在url中,后台通过占位符接收参数 /{id}
  • 3.通过post提交方式将form表单中的数据序列化后传递到后台。
  • 4.通过post提交方式将form表单的类型是 json
  • 5. 前台将普通数据转换为json

    1.发送get请求将参数通过?拼接在url后面

    $.ajax({
            url: "/order/userPage?page="+page+"&pageSize="+pageSize,    //请求的url地址  
            cache: "false",   //设置为false将不会从浏览器中加载请求信息
            async: "true",    //true所有请求均为异步请求
            dataType: "json", //请求返回数据的格式
            type:"get",      //请求方式
     
    上面等同于==>>
    async initData(){
     
       paging: {
          page: 1,
          pageSize: 5
       }
       const res = await orderPagingApi(this.paging) 
    }
     
    function orderPagingApi(data) {
        return $axios({
            'url': '/order/userPage',
            'method': 'get',
            //请求参数
            params: {...data}
        })
     
    上面等同于==>>
    async initData(){
     
       paging: {
          page: 1,
          pageSize: 5
       }
       const res = await orderPagingApi(this.paging) 
    }
     
    function orderPagingApi(data) {
        return $axios({
     
            'url': '/order/userPage',
     
            'method': 'get',
     
            'data': data
        })
     
    

    后端接收参数

    前端给后端传数据的几种方式,前端给后端传数据的几种方式,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,设置,前端,地址,第1张
    (图片来源网络,侵删)
    @GetMapping("/order/userPage")
     
    @ResponseBody
     
    public R userPage(Integer page,Integer pageSize){}
     
    或
     
    @GetMapping("/order/userPage")
     
    @ResponseBody
     
    public R userPage(@RequestParam("page")Integer page,@RequestParam("pageSize")Integer pageSize){}
    

    2.将参数拼接在url中,后台通过占位符接收参数 /{id}

    async initData(){
       
        const res = await addressFindOneApi(params.id)
    }
     
    ​​​​​​​function addressFindOneApi(id) {
      return $axios({
        'url': `/addressBook/${id}`,
        'method': 'get',
      })
    }
    

    后端接收参数

    @GetMapping("/addressBook/{id}")
    @ResponseBody
     
    public R backList(@PathVariable("id")Long id){}
    

    3.通过post提交方式将form表单中的数据序列化后传递到后台。

    async initData(){
     
        const res =await formAjax();
     
    }
     
    function formAjax() {
     
           $.ajax({
     
           url: "/login", 
     
           type: "post", 
     
           data: $("#form").serialize(),  // 对id为form的表单数据进行序列化并传递到后台
    

    后端接收参数

    @RequestMapping("/login")
     
    @ResponseBody
     
    //form表单的数据与User实体类的数据相对应
     
    public String login (User user) {}
    

    4.通过post提交方式将form表单的类型是 json

    async initData(){
     
        const res =await formAjax();
     
    }
     
    function formAjax() {
     
           $.ajax({
     
           url: "/login", 
     
           type: "post", 
           
           contentType: 'application/json',
    

    后端接收参数

    @RequestMapping("/login")
     
    @ResponseBody
     
    //form表单的数据与User实体类的数据相对应
     
    public String login ( @RequestBody User user) {}
    

    5. 前台将普通数据转换为json

    async initData(){
     
       paging: {
          page: 1,
          pageSize: 5
       }
       const res = await orderPagingApi(this.paging) 
    }
     
    function orderPagingApi(data) {
        return $axios({
     
            'url': '/order/userPage',
     
            'method': 'post',
     
             data: JSON.stringify(data),
        })
    

    后台接收参数

    @GetMapping("/order/userPage")
    @ResponseBody
     
    public R userPage(@RequesBody Map map){
     
          Integer page = map.get("page");
            
          Integer pageSize = map.get("pageSize");   
     
    }
     
    或  ==>>
    //假设PageInfo类中有属性与其相对应
    @GetMapping("/order/userPage")
    @ResponseBody
     
    public R userPage(@RequesBody PageInfo pageInfo){
     
         Integer page = pageInfo.getPage();
     
         Integer pageSize = pageInfo.getPageSize();
    }
    
    前端给后端传数据的几种方式,前端给后端传数据的几种方式,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,设置,前端,地址,第2张
    (图片来源网络,侵删)
    前端给后端传数据的几种方式,前端给后端传数据的几种方式,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,设置,前端,地址,第3张
    (图片来源网络,侵删)

免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,人围观)

还没有评论,来说两句吧...

目录[+]