2012-05-26 86 views
1

我想做jQuery删除Ajax,但得到HTTP错误400. 我正在寻找,我看到有一些问题与删除方法在jQuery中。春季3 jquery阿贾克斯删除

这是我的js,控制器(spring 3)和来自Chrome的请求。

如果你现在是什么错误请告诉,或给一些链接。

$.ajax({ 
       url: 'additions/cancelUpload', 
       type: 'DELETE', 
       data: {filename : ui.draggable.get(0).file.name}, 
       success: function (res) { 
        alert(res); 
       } 
      }); 

执行不经过这里:

@RequestMapping(value = "cancelUpload", produces="text/html") 
    @ResponseBody 
    public String cancelUpload(@RequestParam("filename")String filename, HttpSession session){ 
... 
} 

要求:

 
Request URL:http://localhost:8080/WebStore/additions/cancelUpload 
Request Method:DELETE 
Status Code:400 Bad Request 
Request Headersview source 
Accept:*/* 
Accept-Charset:windows-1251,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:uk-UA,uk;q=0.8,ru;q=0.6,en-US;q=0.4,en;q=0.2 
Connection:keep-alive 
Content-Length:17 
Content-Type:application/x-www-form-urlencoded 
Cookie:JSESSIONID=A7DF5CB262C460961BC7B5C7DCB23052 
Host:localhost:8080 
Origin:http://localhost:8080 
Referer:http://localhost:8080/WebStore/items?form 
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1 
1X-Requested-With:XMLHttpRequest 

Form Dataview URL encoded 
filename:6648.jpg 

响应头

 
Connection:close 
Content-Length:1043 
Content-Type:text/html;charset=utf-8 
Date:Sat, 26 May 2012 10:03:58 GMT 
Server:Apache-Coyote/1.1 
X-TraceId:4fc0a8f8-46 
X-TraceUrl:/insight/services/traces/4fc0a8f8-46?type=json 
+0

看来你设置的内容类型为文本的方法/ html和服务要求它是应用程序/ x-www-form-urlencoded – toniedzwiedz

+0

但我确实从js发送Content-Type:application/x-www-form-urlencoded。 并生产=“text/html”是服务器的响应。 我试图改变它,因为你说,但它是行不通的。 –

+0

当我更改POST请求时,一切正常。 –

回答

2

我设法recive请求作为DELETE

@RequestMapping(value = "cancelUpload", produces="text/html" method = RequestMethod.DELETE) 

,但做到这一点与

$.ajax({ 
      url: 'additions/cancelUpload', 
      type: 'POST', 
      data: {filename : ui.draggable.get(0).file.name, _method: 'DELETE'}, 
      success: function (res) { 
       alert(res); 
      } 
     }); 

您可以阅读_method:“删除” POST请求的参数http://blog.springsource.org/2009/03/08/rest-in-spring-3-mvc/

如果一些身体现在怎么送真正删除Ajax请求。请告诉我

+0

我在春季文档中发现了有关_method参数的提及 http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/view.html#rest-方法转换 –

+0

您可以接受您自己的答案,以便将此问题记录为已解决.... –

1

我发送删除请求的JavaScript是这样的:

var delete = document.getElementById('#delete'); 
var url = "url"; 
var http = new XMLHttpRequest();  
var params = ""; 
delete.addEventListener('click', function() { 
    http.open("DELETE", url, true);  
    http.setRequestHeader("Content-type", "application/json"); 
    http.onreadystatechange = function() { 
     if(http.readyState == 4 && http.status == 200) { 
      window.location = url; 
     } 
    } 
    http.send(params); 
}); 

并在控制器

@RequestMapping(value="url", method = RequestMethod.DELETE, produces = "application/json") 
@ResponseBody 
public ResponseEntity<String> delete() { 
    return new ResponseEntity<String>(HttpStatus.OK); 
}