【问题标题】:How do I pass ajax calls to Controller Action method?如何将 ajax 调用传递给 Controller Action 方法?
【发布时间】:2016-04-18 08:54:54
【问题描述】:

如果有人可以使用 Jquery 解释 Ajax 调用并将其传递给控制器​​方法,那就太好了。 用一个解释 句法 示例

// Using the core $.ajax() method
$.ajax({

    // The URL for the request
    url: "post.php",

    // The data to send (will be converted to a query string)
    data: {
        id: 123
    },

    // Whether this is a POST or GET request
    type: "GET",

    // The type of data we expect back
    dataType : "json",
})
  // Code to run if the request succeeds (is done);
  // The response is passed to the function
  .done(function( json ) {
     $( "<h1>" ).text( json.title ).appendTo( "body" );
     $( "<div class=\"content\">").html( json.html ).appendTo( "body" );
  })
  // Code to run if the request fails; the raw request and
  // status codes are passed to the function
  .fail(function( xhr, status, errorThrown ) {
    alert( "Sorry, there was a problem!" );
    console.log( "Error: " + errorThrown );
    console.log( "Status: " + status );
    console.dir( xhr );
  })
  // Code to run regardless of success or failure;
  .always(function( xhr, status ) {
    alert( "The request is complete!" );
  });

如果有人可以使用 Jquery 解释 Ajax 调用并将其传递给控制器​​方法,那就太好了。 用一个解释 句法 示例

【问题讨论】:

  • 到底什么不清楚?
  • 我想知道如何使用 Ajax JQuery 在 asp.net mvc 中调用控制器中的操作方法。 @弗拉基米尔
  • “语法示例” - w3schools.com/php/php_ajax_php.asp 用于 ajax 语法。环顾四周
  • 查看我的编辑。我想知道 ajax 中的函数,如 url、数据类型等。@VelimirTchatchevsky
  • @raghav 你有没有尝试用谷歌搜索任何例子?来自简单谷歌搜索“aso.net mvc ajax jquery”的第一个链接将返回您需要的一切 - mikesdotnetting.com/article/220/…

标签: jquery ajax asp.net-mvc asp.net-ajax


【解决方案1】:

首先,您应该先尝试自己。然后,如果您有任何问题。发布您的代码。顺便说一句,这里是一个例子:

 $.ajax({
                    url: "/Home/Method",  `// Here you specify the action method.Here Home is a controller and method is action method name.`
                    type: "Get",`When you want to get something from server, then Use GET type, If you want to save or post some data to server, then use POST type`
                    contentType: "application/json; charset=utf-8",


      contentType is the type of data you're sending, so application/json; charset=utf-8 is a common one, as is application/x-www-form-urlencoded; charset=UTF-8, which is the default.dataType is what you're expecting back from the server: json, html, text, etc. jQuery will use this to figure out how to populate the success function's parameter.
                    data: {id:id} // `If you want to send some parameter as mentioned in action method parameter. The name of parameter should be same.`
                    cache: false, `cache:true only works with GET and HEAD request. If you want to cache in the browser,then you set it true.`
                    async: true, `async true means you are doing things parallel.You set async to false, when you need that ajax request to be completed before the browser passes to other codes:`
                    success: function (data) {


 It is because Ajax is asynchronous, the success or the error function will be called later, when the server answer the client. So, just move parts depending on the result into your success function  
                    },
                    error: function () {
                        If request failed, it comes here.
                    }
                });

这是你的 Action 方法

[HttpGet]
        public ActionResult Method(int Id)
        {

          //Do your stuff here
            return Json(""); // return some thing
        }

注意:我是为 GET 写的。根据您的情况,它可能是 POST。

【讨论】:

  • 成功和错误应该写什么? Json 有什么用?
  • Json 用于返回一些数据。在ajax,成功,你会得到这个数据。如果你想从服务器获取一些数据。您返回并成功进行操作。如果 ajax 调用由于某种原因失败,它将进入通常显示消息的错误属性。
  • 谢谢。请您帮我解决示例代码中的 cmets 问题,以便我理解
  • 互联网上有很多资料,您可以从中获取有关 ajax 的信息。它会比我告诉你的要好。
  • 我希望您在上面的示例中添加 cmets 以便更好地理解,或者您能给我任何链接来了解这个主题。我在 google 中搜索过,但还没有找到一个好的。
【解决方案2】:

例子

function SendData() {
    var Parameters =
    {
       ID: "123",

    };
    $.ajax({
        url: "@Url.Action("Index", "Home")",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: Parameters ,
        dataType: "json",
        success: function (data) {

        },
        error: function (e) {

        }
    });
};

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多