如何将ajax调用传递给Controller Action方法?

时间:2016-04-18 08:54:54

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

如果有人能够使用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调用并将其传递给控制器​​方法,那将是很棒的。 解释一下 句法 实施例

2 个答案:

答案 0 :(得分:3)

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

 $.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.
                    }
                });

这是你的行动方法

[HttpGet]
        public ActionResult Method(int Id)
        {

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

注意:我写了GET。它可能是POST,具体取决于您的方案。

答案 1 :(得分:1)

实施例

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) {

        }
    });
};