0

これは私のjQueryajax関数です

//delete individual row
jQuery('.stdtable img.delete').click(function(){
    var c = confirm('Continue delete?');
    if (c) {
        jQuery.ajax({
            url: 'market/delete/14',
            type: 'POST',
            success: function () {
                alert('success');
            },
            error: function () {
                alert("error");
            }
        });
    };
    return false;
});

そしてこれは私のコントローラーです

[HttpPost]
public ActionResult Delete(int id)
{
    Using<MarketService>().Delete(int);
    return View();
}

コードは私のコントローラーを呼び出しており、すべてが完璧に機能します。しかし、jQuery関数は常に「エラー」を返します。

なにが問題ですか?

4

2 に答える 2

1

コントローラのコードもコンパイルされましたか?タイプであるintではなく、 idを使用して削除する必要があります

[HttpPost]
public ActionResult Delete(int id)
{
   Using<MarketService>().Delete(id);
   return View();
}

また、JavaScriptを使用せずにアクションをテストするには、ツールFiddlerを使用します。

于 2013-01-03T14:56:19.970 に答える
1

JQuery

//delete individual row
jQuery('.stdtable img.delete').click(function(){
    var c = confirm('Continue delete?');
    if (c) {
        jQuery.ajax({
            url: 'Market/Delete/14', // '@Url.Action("Delete","Market")' is better. Also you should use data attribute for ajax. like this - data : { id : 14 }
            type: 'POST',
            success: function (data) {
                alert(data.Message);
                alert(data.Success);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }
        });
    };
    return false;
});

ajax URLで、market / deleteを使用しました。コントローラー名で、「Delete」は大文字で始まります。それらの間にはケースの感度があります。

コントローラ

[HttpPost]
public ActionResult Delete(int id)
{
   var result=new { Success="True", Message="Success"};
   Using<MarketService>().Delete(id);
   return Json(result, JsonRequestBehavior.AllowGet);
}
于 2013-01-03T15:12:33.337 に答える