JQuery ajax メソッドを介して ASP.NET MVC actionMethod を呼び出そうとしています。私のコードは次のとおりです。
$('.Delete').live('click', function() {
var tr = $(this).parent().parent();
$.ajax({
type: 'DELETE',
url: '/Routing/Delete/' + tr.attr('id'),
contentType: 'application/json; charset=utf-8',
data: '{}',
dataType: 'json',
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Error: " + textStatus + " " + errorThrown);
alert(XMLHttpRequest.getAllResponseHeaders());
},
success: function(result) {
// Remove TR element containing waypoint details
alert("Success");
$(tr).remove();
}
});
});
私のアクションメソッドは次のとおりです。
[AcceptVerbs(HttpVerbs.Delete)]
public string Delete(int id)
{
// Deletion code
return " ";
}
content-length が 0 の場合、問題が発生する可能性があることをどこかで読んだときに、空の文字列を返します。戻り値の型が文字列の場合、「エラー: エラー未定義」という警告ボックスが表示され、2 番目の警告ボックスは空です。
戻り値の型を void にすると、「Error: parsererror undefined」というアラートが表示され、2 つ目のアラートは次のようになります。
Server: ASP.NET Development Server/9.0.0.0
Date: Wed, 22 Jul 2009 08:27:20 GMT
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 1.0
Cache-Control: private
Content-Length: 0
Connection: Close