简体   繁体   中英

How to use Ajax with asp.net MVC4

I am using X-Editable Plugin in Asp.net with jquery Datatables to edit my table. I'm using jQuery $.ajax() Method to make a call to a method in the controller but it's not hitting that method when this function is called. Anyone know how to work jQuery $.ajax() Method?

$('#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;
           }
       });

Control method:

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

Error message :

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
var urlj = "TestMethod"

Is not a valid URI.

Change it to http://://TestMethod however your set up your environment.

Just try to access the TestMethod in your browser if you type in the service URL/TestMethod, it should actually give you some reasonable message.

Your URL should be:

var urlj = '/YOUR_CONTROLLER_NAME/TestMethod'

Your test method must be like this:

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

尝试这个:

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

By changing the Url to this also works ! Give it a try,

url: '@Url.Action("ActionName","ControllerName")',

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM