JQuery $ .ajax通过发送POST接收400 Bad请求

时间:2014-09-20 13:16:36

标签: jquery ajax json spring java-ee

在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"
});

Spring控制器

@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

1 个答案:

答案 0 :(得分:1)

我会说这是因为你的处理程序方法期望一个@PathVariable命名id可以转换为Long,但是在你的POST请求中,你没有发送那个参数,而你的@RequestMapping中你没有为id。

定义了一个URI模板变量

以下内容包含id作为URI模板变量,只要您传递的值可以转换为Long,请求应由请求处理程序方法处理。

@RequestMapping(value = "/taskaction/send/{id}", method = RequestMethod.POST, produces = "application/json")

只有我无法评论的其他内容(可能会得到负面评论)是使用Map作为@RequestBody。根据我的经验,使用具有在其上定义的字段的具体对象类型更好。我认为你真正的问题是你的@RequestMapping和@PathVariable。

相关问题