在jquery ajax上持续接收400(错误请求)到MVC控制器。我试图发送带有和不带有JSON.stringify ...的简单数据,数组,但我的想法耗尽了。还有什么呢?
Ajax发送
$.ajax({
type: 'POST',
url: '../../taskaction/send',
data: JSON.stringify({idTaskAction: 2, actioname: 3}),
success: function (data, textStatus, jqXHR) {
console.log('@Success sending action status: ' + textStatus);
},
error: function(jqXHR, textStatus, errorThrown){
console.log('@Error sending action status: ' + textStatus);
},
contentType: "application/json; charset=utf-8",
dataType: "json"
});
弹簧控制器
@RequestMapping(value = "/taskaction/send",
method = RequestMethod.POST,
produces = "application/json")
@ResponseBody
public Map<String, Object> sendAction(Principal principal,
@RequestBody Map<String, Object> data, @PathVariable Long id) {
logger.info("Task controller /taskaction/send ...");
String actionname = (String) data.get("actionname");
System.out.println("*****>>>>>>" + actionname );
Map<String, Object> rdata = new HashMap<String, Object>();
TaskAction action = null;
rdata.put("success", true);
return rdata;
}
检查员的HTTP请求
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/myTasks/taskaction/send
Request Method:POST
Status Code:400 Petición incorrecta
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate
Accept-Language:es-ES,es;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:33
Content-Type:application/json; charset=UTF-8
Cookie:JSESSIONID=9073BF5FA653C2C673AD9BCB787732C3
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/myTasks/task/upd/8
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payloadview source
{idTaskAction:2, actionname:3}
actionname: 3
idTaskAction: 2
Response Headersview source
Connection:close
Content-Language:es
Content-Length:990
Content-Type:text/html;charset=utf-8
Date:Sat, 20 Sep 2014 13:19:31 GMT
Server:Apache-Coyote/1.1
我想说这是因为您的处理程序方法需要一个@PathVariable命名ID,该ID可以转换为Long,但是在POST请求中,您没有发送该参数,并且在@RequestMapping中,您尚未定义URI ID的模板变量。
类似于以下内容的ID包含作为URI模板变量的内容,只要您传递的值可以转换为Long,请求就应该由您的请求处理程序方法处理。
@RequestMapping(value = "/taskaction/send/{id}", method = RequestMethod.POST, produces = "application/json")
我无法评论的其他事情(可能会得到负面评论)只是将Map用作@RequestBody。以我的经验,使用在其上定义了字段的具体对象类型效果更好。我认为您的真正问题是@RequestMapping和@PathVariable。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句