簡體   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