【问题标题】:JQuery send JSON to Spring MVC controllerJQuery 将 JSON 发送到 Spring MVC 控制器
【发布时间】:2012-01-29 01:09:34
【问题描述】:

我无法将带有 JQuery Ajax 的 JSON 对象发送到 Spring MVC 控制器。这是我的控制器的方法的定义:

@Controller
@RequestMapping(value = "InboxViewTemplate")
public class InboxViewController {

@ResponseBody
    @RequestMapping(value = "updateInboxView")
    public String updateInboxView(HttpServletRequest request, InboxView inboxView) {
...
}

然后我正在尝试调用此请求:

$.ajax({
            dataType: 'json',
            contentType: "application/json",
            url: ctx + "/InboxViewTemplate/updateInboxView",
            data: ({inboxView : {createUser:"dave"}}),
            success: function(data) {
                $("#updateInboxView").html(data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR + " : " + textStatus + " : " + errorThrown);
            }
          });
      }

但未传递 JSON 对象。有人能帮我吗?提前致谢。

【问题讨论】:

    标签: jquery ajax json spring model-view-controller


    【解决方案1】:

    首先,您的控制器不知道在哪里寻找 InboxView。是请求参数吗?路径参数?请求正文?

    其次,您可能希望将 json 请求类型更改为 POST 或 PUT,因为您正在更新数据而不仅仅是检索数据。

    所以是这样的:

    @Controller
    @RequestMapping(value = "InboxViewTemplate")
    public class InboxViewController {
    
    @ResponseBody
        @RequestMapping(value = "updateInboxView", method = RequestMethod.POST)
        public String updateInboxView(HttpServletRequest request, @RequestBody InboxView inboxView) {
        ...
    } 
    

    $.ajax({
                dataType: 'json',
                contentType: "application/json",
                url: ctx + "/InboxViewTemplate/updateInboxView",
                type: 'POST',
                data:  JSON.stringify({inboxView : {createUser:"dave"}}), //if no JSON is available use the one from https://github.com/douglascrockford/JSON-js
                success: function(data) {
                    $("#updateInboxView").html(data);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR + " : " + textStatus + " : " + errorThrown);
                }
              });
          }
    

    应该可以。

    我在这里假设您的 json 消息转换器配置正确。

    编辑 意味着你有:

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

    或其他消息转换器的等效项在您的 spring xml 配置中。

    【讨论】:

    • 感谢您的回复,现在我收到 404 错误。我也试过 var obj = jQuery.parseJSON('{"createUser":"John"}');并将这个对象传递给 mvc 方法。似乎无法识别 RequestBody 对象。我应该尝试其他配置吗?还是我的ajax请求不正确?非常感谢。
    • @carlo 首先检查您是否可以连接到您的控制器(使用 curl 或 telnet)。然后检查您的服务器日志中是否有任何错误。检查您的弹簧上下文中是否有 。最后检查您的消息转换器是否配置正确。查看我的编辑。
    • 现在发生了一个我无法理解的想法:如果我删除注释@RequestBody,则调用服务而不是如果存在注释我有404错误。这种情况如何调试?谢谢。
    • @carlo 嗯,确保您将 jquery 的请求类型更改为“post”。否则jquery会尝试在url中编码json,我认为spring还不知道如何处理它。
    • 是的,请求的类型是这样的:$.ajax({ dataType: "json", contentType: "application/json", url: ctx + "/InboxViewTemplate/updateInboxView", type: " POST”,缓存:假,数据:{名称:“appId”},成功:函数(数据){$(“#updateInboxView”).html(数据);},错误:函数(jqXHR,textStatus,errorThrown){ alert(jqXHR + " : " + textStatus + " : " + errorThrown); } });
    【解决方案2】:

    你有没有看过你的日志文件来找出404的原因。我怀疑你需要将jakson相关的jar文件放入你的lib中。

    【讨论】:

      猜你喜欢
      • 2012-09-09
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 2014-10-31
      • 2016-09-07
      • 2013-01-11
      相关资源
      最近更新 更多