【问题标题】:Receiving parameter in controller from Ajax call in Spring在 Spring 中从 Ajax 调用接收控制器中的参数
【发布时间】:2014-08-14 11:54:34
【问题描述】:

在我的 spring 项目中,我通过 Ajax 调用发送请求,如下所示:

function doAjaxPost(currentPage) {

    var appName = document.searchForm.txtZipFile.value;  
    var e = document.getElementById("selectStatus");
    var appStatus = e.options[e.selectedIndex].text;        


    $.ajax({  
     type : "GET",   
     url : "http://localhost:8080/ preListOnSearch.do",   
      data : "currentPage=" + currentPage + "&appName=" + appName + "&appStatus="  
       + appStatus, 
       cache: false,

     success : function(response) {           
      alert(response);   
     },  

     error : function(e) {  
      alert('Error: ' + e);   
     } 

    }); 
   }  

在我的控制器中,我编写了如下方法:

@RequestMapping(value = "/preListOnSearch", method=RequestMethod.GET)
public String preTestDataolx(@PathVariable("siteId") String siteId,                  @PathVariable(value = "currentPage") String currentPage,
         @RequestParam(value = "appStatus") String appStatus) {
    System.out.println(appStatus);
    return "/preTestData";  
}

但这给了我错误。当我从方法定义中删除 RequestParams 时,它工作正常。所以我只想知道如何访问控制器中的ajax调用参数。

【问题讨论】:

  • 它工作正常,没有错误

标签: java ajax spring jakarta-ee spring-mvc


【解决方案1】:

尝试将数据设置为 JS 对象:

$.ajax({  
   type : "GET",   
   url : "http://localhost:8080/ preListOnSearch.do",   
   data : {currentPage: currentPage, appName: appName, appStatus: appStatus},
   cache: false,

【讨论】:

    猜你喜欢
    • 2018-03-24
    • 1970-01-01
    • 2014-05-16
    • 2018-12-16
    • 1970-01-01
    • 2013-05-18
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多