javascript 如何在Spring MVC应用程序中使用Jquery发送多个参数Ajax请求。?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21978685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:08:17  来源:igfitidea点击:

How to send multiple parameters Ajax request with Jquery in Spring MVC Application.?

javascriptjqueryajaxspring-mvcjquery-autocomplete

提问by Priyan RockZ

Here is my relevant codes.my out put is like this. enter image description here

这是我的相关代码。我的输出是这样的。 在此处输入图片说明

I need to send region and tsrId as parametersto query. here is my code

我需要发送region 和 tsrId 作为参数进行查询。这是我的代码

jsp

jsp

Here is my ajax request with jquery

这是我使用 jquery 的 ajax 请求

<script type="text/javascript">
      $(document).ready(function() {
            var region = document.getElementById('region').value;
            var tsrId = document.getElementById('tsrId').value;
            $('#tsrId').autocomplete({
                serviceUrl: 'getTsrId.html',
                data: ({queryData : {region:region,tsrId:tsrId}}),
                //delimiter: ",",
                transformResult: function(response) {
                return {suggestions: $.map($.parseJSON(response), function(item) {return { value: item.name, data: item.id };
                   })};}});});
</script>    

here is the HTML form

这是 HTML 表单

  <td>Region</td>
  <td><input type="text" name="region" id="region"><div class="autocomplete-suggestions"></div></td>
  <td>TSR ID</td>
  <td><input type="text" name="tsrId" id="tsrId" maxlength="8"><div class="autocomplete-suggestions2"></div></td>

here is my controller

这是我的控制器

@RequestMapping(value = "/getTsrId", method = RequestMethod.GET)
public @ResponseBody List<TSRMaster> getTsrId(@RequestParam String tagName,@RequestBody QueryData queryData) {
    List<TSRMaster> tsrMasterList=new ArrayList<TSRMaster>();
    tsrMasterList=gpsdao.getTsrIdList(queryData.getRegion(),queryData.getTsrId());
    return tsrMasterList;
}       

here is my bean class for requestMapping

这是我用于 requestMapping 的 bean 类

public class QueryData {

    private String region;
    private String  tsrId;

    public String getRegion() {
        return region;
    }
    public void setRegion(String region) {
        this.region = region;
    }
    public String getTsrId() {
        return tsrId;
    }
    public void setTsrId(String tsrId) {
        this.tsrId = tsrId;
    }

}

Please help me to sort out this issue..is there any other alternative solution, please mention that path below thanks.

请帮我解决这个问题..是否有其他替代解决方案,请在下面提及该路径,谢谢。

回答by ced-b

The only way I have been able to make this work so far is to call JSON.stringify()on the client, which turns a JavaScript Object into a JSON String. (To be cross browser compatible you would need json2.js)

到目前为止,我能够完成这项工作的唯一方法是调用JSON.stringify()客户端,它将 JavaScript 对象转换为 JSON 字符串。(要跨浏览器兼容,您需要json2.js

Then you send this as a String parameter to Spring and parse it there using the Hymansonlibrary.

然后你将它作为字符串参数发送到 Spring 并使用Hymanson库在那里解析它。

Sample Code:

示例代码:

Java Script

Java脚本

data: ({queryData : JSON.stringify({region:region,tsrId:tsrId}})),

Java

爪哇

RequestMapping(value = "/getTsrId", method = RequestMethod.GET)
public @ResponseBody List<TSRMaster> getTsrId(@RequestParam String tagName,@RequestParam String queryData) {

    ObjectMapper myMapper = new ObjectMapper();
    QueryData myQueryData = myMapper.readValue(queryData, QueryData.class);

    List<TSRMaster> tsrMasterList=new ArrayList<TSRMaster>();
    tsrMasterList=gpsdao.getTsrIdList(myQueryData.getRegion(),queryData.getTsrId());
    return tsrMasterList;
}  

回答by slslprovider

You can use Hymanson framework for JSON java transformation. Then you can use following method to send data to the controller from the view.

您可以使用 Hymanson 框架进行 JSON java 转换。然后您可以使用以下方法将数据从视图发送到控制器。

Add Hymanson jars to the project.

将 Hymanson jar 添加到项目中。

Hymanson-core-2.0.5 Hymanson-databind-2.0.5 Hymanson-annotation-2.0.5

Hyman逊核心2.0.5 Hyman逊数据绑定2.0.5 Hyman逊注释2.0.5

Add following code to WebApplicationContext.xml

将以下代码添加到 WebApplicationContext.xml

<bean id="HymansonMessageConverter" class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="HymansonMessageConverter"/>
        </list>
    </property>
</bean>

Ajax call

Ajax 调用

 $.ajax({
        url: getTsrId.html,
        type: 'GET',
        data: "region=" + region + "&tsrId=" + tsrId,           
        dataType: "json", 
        success: function(response){

//response here
}
});

Controller

控制器

 @RequestMapping(value = "/getTsrId", method = RequestMethod.GET,produces="application/json")
    public @ResponseBody List<TSRMaster> getTsrId(
                @ModelAttribute(value = "queryData") QueryData queryData) {
        List<TSRMaster> tsrMasterList = new ArrayList<TSRMaster>();
        tsrMasterList = gpsdao.getTsrIdList(queryData.getRegion(),queryData.getTsrId());
        return tsrMasterList;
    }