代码之家  ›  专栏  ›  技术社区  ›  menteith

如何使用angularjs将字符串作为spring'@requestparam发送?

  •  0
  • menteith  · 技术社区  · 6 年前

    在我的后端(Spring控制器)中,我有一个方法 @RequestParam String sentence 作为请求参数。我想通过发送适当的参数向这个方法发送一个请求(post)。

    在安格拉尔的书中,我写道:

    $http.post('http://localhost:8080/wordCount', '?sentence=blah')
    

    但是后端返回错误 Required String parameter 'sentence' is not present"

    这是我的控制器:

    @RequestMapping(value = "wordCount", method = RequestMethod.POST)
    public String count(@RequestParam(value = "sentence") String sentence) {
    
            //omitted
    }
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Arpit Agrawal    6 年前

    基本上 @RequestParam 用于查询字符串参数(URL查询参数)。 @RequestBody 是请求后正文。 现在$http.post用法是 mentioned 作为post(url,data,[config]),其中data是post数据,可以使用 @ RequestBody . 但既然你需要 @ RequestParam ,然后需要传递附加到URL的查询字符串参数,如 $http.post('http://localhost:8080/wordCount?sentence=blah',{}) .

        2
  •  0
  •   georgeawg    6 年前

    URL参数是使用 params 性质 config :

    var url = "http://localhost:8080/wordCount";
    var params = { sentence: "blah" };
    var config = { params: params };
    
    $http.post(url, data, config);
    

    有关详细信息,请参阅 AngularJS $http Service API Reference - post