Home  >  Q&A  >  body text

angular.js - angularjs到底如何传参到springMVC

话说angular的传参转变为json格式,而又需要springMVC有相关的实体类才能用@requestBody去接收。但是如果传的是一般的参数呢,比如一个数组,一个数字id?

当然,传一个数字id可以从请求参数路径传过去。但是,就是传一般的随机的参数,比如data={“xx”,xxx}

和明显,springMVC端是不能像接收jquery时候用@requestParam接收的。

网上看了一大片,说要改请求的header的contentType什么的,可是,设置了也没用.......怎么办?

$http({
url:"ts",
data={“xx”,xxx},
type:"POST"
})
........

spirngMVC端如何接收data={“xx”,xxx},

为情所困为情所困2736 days ago566

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-05-15 17:03:04

    Front desk code:

    angular.module('app')
        .factory('Email', function ($resource) {
            return $resource('api/emails/:id', {}, {
                'query': { method: 'GET', isArray: true},
                'get': {method: 'GET'},
                'update': { method:'PUT' }
            });
        });

    Background reception

    @RequestMapping(value = "/emails",
                method = RequestMethod.POST,
                produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EmailDTO> createEmail(@RequestBody EmailDTO emailDTO) {.....} 
    
              

    reply
    0
  • PHP中文网

    PHP中文网2017-05-15 17:03:04

    The problem is not how Angular passes parameters to the backend, it mainly depends on how you implement it in the backend. It is enough for angular to provide an interface in the background. Angular can directly $http adjust the background interface to pass the parameters to the background.

    $http.get("mainUrl"+parameter).then();
    

    In this way, the parameters can be passed to the backend. As for whether to get or post or anything else, it mainly depends on the backend.

    reply
    0
  • Cancelreply