Jquery AJAX DELETE problem
Any ideas why this does not pass 'id' form value?
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" 开发者_如何转开发language="javascript">
var deleteUser = function () {
var id = this.id.split('lnk_delete_user_')[1];
console.log(id); //prints frickin "2"
$.ajax({
type: "DELETE", url: '<%= Url.Action("DeleteUser") %>', data: "id=" + id,
success: function (data) {
window.location.href = '<%= Url.Action("Users") %>'
}
});
};
$(function ($) { $("a[id^=lnk_delete_user_]").confirm().click(deleteUser); });
</script>
Server side does not receive 'id' argument. What might be wrong?
Did check that right before $.ajax(...)
id equals 2.
Tried rewriting data: "id="+id,
to data: {id:id}
, nothing changes.
This is how my action looks like:
[HttpDelete]
[Authorize(Roles="admin")]
public ActionResult DeleteUser(string id){
//does not get even so far
}
For the moment, you need to POST and not DELETE, like this:
$.ajax({
type: "POST",
url: '<%= Url.Action("DeleteUser") %>',
data: { id: id },
success: function (data) {
window.location.href = '<%= Url.Action("Users") %>'
}
});
jQuery won't properly serialize data parameters for DELETE requests until 1.4.4, there is already a fix in place for this...so if you can wait for the jQuery 1.4.4 bugfix release, this should be resolved.
In your script the id javascript variable is not defined. Also I would recommend you using data hash instead of string concatenation as this will ensure proper encoding:
$.ajax({
type: 'DELETE',
url: '<%= Url.Action("DeleteUser") %>',
data: { id: '123' },
success: function (data) {
window.location.href = '<%= Url.Action("Users") %>';
}
});
Where your controller action looks like this:
[HttpDelete]
public ActionResult DeleteUser(string id)
{
throw new NotImplementedException();
}
UPDATE:
It seems that the problem comes from the fact that you are using jquery from here: http://code.jquery.com/jquery-latest.min.js
When you include it from google CDN it works fine:
http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js
If id is your a value inside your ASP script then use this:
$.ajax({
type: 'DELETE',
url: '<%= Url.Action("DeleteUser") %>',
data: { id: <%= id %> },
success: function (data) {
window.location.href = '<%= Url.Action("Users") %>';
}});
精彩评论