Reputation: 480
I am trying to do ajax delete with jquery but getting http error 400. I was searching and I've seen that there is some problems with delete method in jquery.
Here's my js, controller(spring 3) and request from Chrome.
If you now what is the mistake please tell, or give some links.
$.ajax({
url: 'additions/cancelUpload',
type: 'DELETE',
data: {filename : ui.draggable.get(0).file.name},
success: function (res) {
alert(res);
}
});
execution does not passed here:
@RequestMapping(value = "cancelUpload", produces="text/html")
@ResponseBody
public String cancelUpload(@RequestParam("filename")String filename, HttpSession session ){
...
}
request:
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
Response Headers
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
Upvotes: 1
Views: 4096
Reputation: 1413
I sent a delete request with JavaScript this way:
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);
});
And the method in controller
@RequestMapping(value="url", method = RequestMethod.DELETE, produces = "application/json")
@ResponseBody
public ResponseEntity<String> delete() {
return new ResponseEntity<String>(HttpStatus.OK);
}
Upvotes: 1
Reputation: 480
I manage to recive request as DELETE
@RequestMapping(value = "cancelUpload", produces="text/html" method = RequestMethod.DELETE)
but do this with
$.ajax({
url: 'additions/cancelUpload',
type: 'POST',
data: {filename : ui.draggable.get(0).file.name, _method: 'DELETE'},
success: function (res) {
alert(res);
}
});
you can read about _method: 'DELETE' parameter of post request in http://blog.springsource.org/2009/03/08/rest-in-spring-3-mvc/
IF SOME BODY NOW HOW TO SEND REALLY DELETE AJAX REQUEST. PLEASE TELL ME
Upvotes: 2