简体   繁体   English

jQuery $ .ajax通过发送POST接收到400错误请求

[英]JQuery $.ajax by sending POST receiving 400 Bad request

Continually receiving 400 (Bad Request) on jquery ajax post to MVC controller. 在jquery ajax上持续接收400(错误请求)到MVC控制器。 I've tried to send simple data, array, with and without JSON.stringify... and I ran out of ideas. 我试图发送带有和不带有JSON.stringify ...的简单数据,数组,但我的想法耗尽了。 What else could it be? 还有什么呢?

Ajax send 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 controller 弹簧控制器

@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 Request from inspector 检查员的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

I would say it's because your handler method is expecting a @PathVariable named id which can be converted into a Long, however in your POST request, you're not sending in that argument and in your @RequestMapping, you haven't defined a URI template variable for id. 我想说这是因为您的处理程序方法需要一个@PathVariable命名ID,该ID可以转换为Long,但是在POST请求中,您没有发送该参数,在@RequestMapping中,您还没有定义URI ID的模板变量。

Something like the following contains the id as a URI template variable and so long as the value you pass can be converted to a Long, the request should be handled by your request handler method. 类似于以下内容的ID包含作为URI模板变量的内容,只要您传递的值可以转换为Long,请求就应该由请求处理程序方法处理。

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

Only other thing I can't comment on (and might get negative comments for) is using a Map as a @RequestBody. 我无法评论的其他事情(可能会得到负面评论)只是将地图用作@RequestBody。 In my experience, using a concrete object type with fields defined on it has worked better. 以我的经验,使用定义了字段的具体对象类型效果更好。 I think your real problem though is your @RequestMapping and @PathVariable. 我认为您的真正问题是@RequestMapping和@PathVariable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM