gpt4 book ai didi

java - 在 Spring Controller 上执行 $.ajax POST 后收到错误请求错误 (400)

转载 作者:行者123 更新时间:2023-12-01 08:14:23 25 4
gpt4 key购买 nike

你好 StackOverFlow

我已经解决这个问题两个多小时了很简单

我正在尝试使用 $.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)
}
})
}

Spring Controller :

@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";
}

}

Spring 配置:

<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');
}

Fiddle 原始请求 header :

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

Fiddle 响应 header

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) 似乎不适用于我的情况!!

关于java - 在 Spring Controller 上执行 $.ajax POST 后收到错误请求错误 (400),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14630139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com