2016-04-18 8 views
-3

Es wäre großartig, wenn jemand Ajax-Aufrufe mit Jquery erklären und an die Controller-Methode übergeben könnte. Erklären Sie mit einem Syntax BeispielWie übergebe ich Ajax-Aufrufe an die Controller Action-Methode?

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

Es wäre toll, wenn jemand kann erklären Ajax JQuery-Anrufe verwenden und dass Verfahren zur Steuerung zu übergeben. Erklären Sie mit einem Syntax Beispiel

+0

Was ist genau das nicht klar? – Vladimirs

+0

Ich möchte wissen, wie Sie Action-Methode in Controller in asp.net mvc mit Ajax JQuery aufrufen. @Vladimirs – raghav

+0

"Syntax Beispiel" - http://www.w3schools.com/php/php_ajax_php.asp für Ajax-Syntax. Schauen Sie sich um –

Antwort

3

Zunächst einmal sollten Sie sich zuerst versuchen. Dann, wenn Sie irgendein Problem haben. Sende deinen Code. BTW hier ein Beispiel:

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

Hier ist Ihre Aktion Methode

[HttpGet] 
     public ActionResult Method(int Id) 
     { 

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

Anmerkung: Ich schrieb für GET. Je nach Szenario kann es POST sein.

+0

Was sollten wir in Erfolg und Irrtum schreiben? Was nutzt Json? – raghav

+0

Bitte beantworten Sie es. – raghav

+0

Json wird verwendet, um einige Daten zurückzugeben. In Ajax, Erfolg, erhalten Sie diese Daten. Wenn Sie Daten vom Server wünschen. Sie kehren zurück und machen den Betrieb erfolgreich. Wenn ein Ajax-Aufruf aus irgendeinem Grund fehlschlägt, wird es in Fehlerattribut kommen, wo normalerweise Nachricht angezeigt wird. – CodeLover

1

Beispiel

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

Können Sie mir Links geben, um über Json zu lernen? Können wir es ohne Json tun – raghav

+0

überprüfen Sie diese http://www.tutorialspoint.com/json/json_tutorial.pdf und http://6.470.scripts.mit.edu/2010/lectures/ajax_json_jquery/ajax_json_jquery_slides.pdf –

+0

Was sollte sein geschrieben in Erfolg und Irrtum? – raghav

Verwandte Themen