gpt4 book ai didi

jquery - Spring Jquery Ajax Post 上的 400 错误请求

转载 作者:行者123 更新时间:2023-12-03 22:31:35 24 4
gpt4 key购买 nike

我在此 POST 请求上收到 400 错误请求。知道这里有什么问题吗?日志为here .

Controller

@Controller
public class AjaxController {
@RequestMapping(value="/addKeys", method=RequestMethod.POST, consumes="application/json; charset=UTF-8")
public ResponseEntity<String> addKeys(@RequestParam(value="keys") ArrayList<Keys> keys){
System.out.println("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"+keys);
}
}

context-Servlet.xml

<beans>
<mvc:annotation-driven />
<context:component-scan base-package="com.canon.fw.controller" />
<bean id="defaultViews" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</beans>

Ajax

tmpList = '[{"key":"camera","label":"Camera"},{"key":"mobile","label":"Mobile"}]';
$.ajax({
type: 'POST',
url: ctx+'/ajx/addKeys',
data: JSON.stringify({"keys": tmpList }),
success: function(r){
if(r.model.status=='success'){
debugger;
//glist.push(elem.key);
//addToList(elem.key, elem.label);
highlightInfoDisc();
}
},
dataType: 'json',
contentType: 'application/json'
});

FireBug - URL

http://localhost:8080/Di/ajx/addKeys

Firebug - 响应 header

Cache-Control   must-revalidate,no-cache,no-store
Content-Length 1384
Content-Type text/html; charset=iso-8859-1
Server Jetty(6.1.26)

Firebug - 请求 header

Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection keep-alive
Content-Length 74
Content-Type application/json; charset=UTF-8
Cookie JSESSIONID=7mymobst47ig1s7uqy2z1fvx4
Host localhost:8080
Referer http://localhost:8080/Di/tiles/entNews.htm
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1
X-Requested-With XMLHttpRequest

Firebug - 来源

{"keys":[{"key":"canon","label":"Canon"},{"key":"ricoh","label":"Ricoh"}]}

Firebug - 响应

"NetworkError: 400 Bad Request - http://localhost:8080/Di/ajx/addKeys"

最佳答案

您正在发布 JSON,而不是表单数据,但您正在尝试读取 ArrayList<Keys>作为表单参数。

尝试更改您的方法签名:

public ResponseEntity<String> addKeys(@RequestParam(value="keys") ArrayList<Keys> keys){

public ResponseEntity<String> addKeys(@RequestBody Keys[] keys){

这可能不起作用,因为您的 JSON 有一个具有 keys 的对象属性是键列表。所以你可以尝试将你的ajax post数据更改为类似

data: JSON.stringify(tmpList)

这样您只需发布列表,而不是将其包装在另一个具有keys元素的对象中。

关于jquery - Spring Jquery Ajax Post 上的 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13075101/

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