如何在asp.net MVC4中使用Ajax

时间:2013-09-26 11:24:33

标签: c# ajax asp.net-mvc-4 datatables jquery-datatables

我在Asp.net中使用X-Editable Plugin和jquery Datatables来编辑我的表格。 我正在使用jQuery $ .ajax()方法来调用控制器中的方法,但是在调用此函数时它不会触及该方法。任何人都知道如何使用jQuery $ .ajax()方法?

$('#example').dataTable();
"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {                  
               $('td', nRow).wrapInner('<a class="xediable-example" href="#"></a>').editable({
                   type: 'text',
                   pk: 1,
                   name: 'test',
                   url: function (params) {                           
                         var urlj = "TestMethod"
                           return $.ajax({
                           type: 'POST',
                           url: urlj,
                           data: JSON.stringify(params),
                           contentType: 'application/json; charset=utf-8',
                           dataType: 'json',
                           async: false,
                           cache: false,
                           timeout: 10000,                              
                       });
                   }
               });                   
               return nRow;
           }
       });

控制方法:

public static string TestMethod(string name, string pk, string value)
{
    return "" ;
}

错误讯息:

Erreur du serveur dans l'application '/'.La ressource est introuvable. Description : HTTP 404. La ressource recherchée (ou l'une de ses dépendances) a peut-être été supprimée ou renommée ou bien elle n'est plus disponible temporairement. Vérifiez l'URL ci-après et assurez-vous qu'elle est correcte. URL demandée: /H41_TitreConge/TestMethod Informations sur la version : Version Microsoft .NET Framework :4.0.30319; Version ASP.NET :4.0.30319.17929

4 个答案:

答案 0 :(得分:0)

var urlj = "TestMethod"

不是有效的URI。

将其更改为http://:// TestMethod 但是你设置了你的环境。

如果您输入服务URL / TestMethod,只需尝试在浏览器中访问TestMethod,它实际上应该给您一些合理的消息。

答案 1 :(得分:0)

您的网址应为:

var urlj = '/YOUR_CONTROLLER_NAME/TestMethod'

您的测试方法必须如下:

[HttpPost]
public JsonResult TestMethod(string name, string pk, string value)
{
    return Json( ... )
}

答案 2 :(得分:0)

试试这个:

var urlj = "@Html.Action("TestMethod")"

答案 3 :(得分:0)

通过更改Url到此也有效!试一试,

url: '@Url.Action("ActionName","ControllerName")',
相关问题