繁体   English   中英

在Spring Controller上的$ .ajax POST之后接收到错误的请求错误(400)

[英]Receiving a Bad Request Error (400) after $.ajax POST on Spring Controller

你好StackOverFlow(s)

我已经有两个多小时了,所以我一直在处理此问题,这很简单

我正在尝试通过使用$ .ajax POST调用将JSON对象发送到Spring Controller

我正在使用AngularJS,但是那点很好

这是服务器和客户端的代码以及spring配置

提前致谢

jQuery的:

    $scope.push = function() {
    $.ajax({
        type: "PUT",
        url:"rest/todo/greeting/",
        data : {id:"1",title:"ajax",description:"ajax"},
        dataType: "json",
        contentType : "application/json",
        success : function(data) {
            $log.info(data)
        }
    })
}

弹簧控制器:

@Controller
@RequestMapping("/todo")
public class TodoController {
@RequestMapping(value = "/greeting", method = RequestMethod.PUT,consumes="application/json",produces="text/html")
public @ResponseBody String push(@RequestBody Todo todo) {
    System.out.println(todo.getTitle());
    return "test";
}

}

弹簧配置:

<mvc:annotation-driven />
<context:component-scan base-package="org.lab.todo.controller" />
<bean id="defaultViews" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<!--
    Spring WEBMVC/REST Controllers
-->
<servlet>
    <servlet-name>todo-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>todo-dispatcher</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Ajax呼叫更新:

    $scope.push = function() {
    var jsonString = {id:"1",title:"ajax",description:"ajax"};
    var Todo = function(){}
    Todo.id = "id";
    Todo.title = "ajax";
    Todo.description = "ajax";
    $.post("rest/todo/greeting",JSON.stringify(Todo),function(response){console.log(response)},'json');
}

小提琴原始请求标头:

POST http://localhost:8080/todo-rest/rest/todo/greeting HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 0
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko)     Chrome/24.0.1312.56 Safari/537.17
Referer: http://localhost:8080/todo-rest/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,fr;q=0.6
Accept-Charset: UTF-8,*;q=0.5

小提琴响应头

HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1048
Date: Thu, 31 Jan 2013 17:11:40 GMT

这就是答案

只需正确编写JSON字符串即可

代替

var jsonString = {id:"1",title:"ajax",description:"ajax"};

我是这样写的

var jsonString = '{"id":"1","title":"ajax","description":"ajax"}';

仍然很奇怪的是,在我的情况下,JSON.stringify(MyObject)似乎不起作用!

如果您收到400状态代码,则只能出于以下原因(来自W3 ):

10.4.1 400错误的请求

由于语法格式错误,服务器无法理解该请求。 客户不应在没有修改的情况下重复请求。

使用Spring,这意味着ajax请求是错误的(请参阅我对数据JSON字符串的评论),或者它无法解析请求主体中的JSON字符串并将其转换为命令对象Todo 因此,Spring有一个可以调用的方法,即push()方法,但是它没有您要传递给它的参数,因此它引发了400 Bad Request。

415 Unsupported Media Type意味着Spring无法找到一种方法来消费您所发送的内容。 您的push() Spring方法使用了consumes="application/json"但您的请求未使用该内容类型。

您可以发布您的web.xml吗? 由于您没有为请求定义后缀,而在末尾加了斜杠,因此您正在对该目录的index.jsp进行请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM