2016-04-18 14 views
-3

誰かがJqueryを使ってAjaxの呼び出しを説明し、それをコントローラメソッドに渡すことができれば幸いです。 誰もがアヤックスのjQueryを使用して呼び出して、コントローラメソッドにそれを渡すように説明できる場合、それは素晴らしいことだ ajax呼び出しをController Actionメソッドに渡すにはどうすればよいですか?

// 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!"); }); 

構文 例
と説明。 は、すべての

+0

? – Vladimirs

+0

Ajax JQueryを使用してasp.net mvcのコントローラでアクションメソッドを呼び出す方法を知りたい。 @Vladimirs – raghav

+0

"構文例" - http://www.w3schools.com/php/php_ajax_php.asp、ajax構文については、あなた自身の周りを見てください –

答えて

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になる場合があります。正確に明らかではないが、何

+0

成功とエラーで何を書くべきでしょうか? Jsonの使い方は? – raghav

+0

答えてください。 – raghav

+0

Jsonはデータを返すために使用されます。 ajaxでは、成功すると、このデータが得られます。サーバからのデータが必要な場合あなたは戻って、成功して操作をします。何らかの理由でajax呼び出しが失敗すると、通常はメッセージが表示されるエラー属性になります。 – CodeLover

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

     } 
    }); 
}; 
+0

Jsonについての学習のリンクを教えてもらえますか? Jsonなしでこれを行うことができます – raghav

+0

このhttp://www.tutorialspoint.com/json/json_tutorial.pdfとhttp://6.470.scripts.mit.edu/2010/lectures/ajax_json_jquery/ajax_json_jquery_slides.pdf –

+0

を確認してください成功とエラーで書かれている? – raghav

関連する問題