2017-02-23 100 views
-2

是否有可能在沒有Jquery的類型腳本/ JavaScript中調用帶有Ajax調用的MVC控制器方法?如果不是如何我可以從JavaScript /類型腳本文件調用控制器方法?從沒有jquery的TypeScript/JavaScript調用MVC控制器方法

考慮一個呼叫控制器方法進行排序,併發送一個排序列到它的方法:

這是TS文件功能:

function SortData() 
    { 
    .... call Controller method and send sortCriteria (FullName) to it 

    } 

,這是控制器的方法:

[Route("sortbycolumn")] 
    public ActionResult SortByColumn(string sortCriteria) 
    { 
     .... Do the sort retun back json result 
    } 
+1

XMLHttpRequest對象 - https://msdn.microsoft.com/en-us/library/ms535874(v=vs.85).aspx – rageit

回答

1

我已經包含了GET的例子和POST +提交/使用接收數據香草JS & AJAX下面。

欲瞭解更多信息,請給this一個閱讀。

祝你好運。

function SortData() { 

    var data; 

    //Post Example 
    var xhttp = new XMLHttpRequest(); 

    xhttp.open("POST", "/Controller/Action", true); 
    xhttp.setRequestHeader("Content-Type", "application/json"); 

    //There are two options for using xhttp.send(): Only keep the ONE that applies to you 

    //Option 1: Submit data to the server 
    xhttp.send(JSON.stringify(params)); 

    //OR 

    //Option 2: Nothing to submit to the server 
    xhttp.send(); 

    xhttp.onload = function(response) { 

     if(response.target.status == 200) { 

      data = JSON.parse(response.target.response); 

     } 

    }; 

    //Get Example 
    var xhttp = new XMLHttpRequest(); 

    xhttp.open("GET", "/Controller/Action", true); 
    xhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); 

    //There are two options for using xhttp.send(): Only keep the ONE that applies to you 

    //Option 1: Submit data to the server 
    xhttp.send(JSON.stringify(params)); 

    //OR 

    //Option 2: Nothing to submit to the server 
    xhttp.send(); 

    xhttp.onload = function(response) { 

     if(response.target.status == 200) { 

      data = JSON.parse(response.target.response); 

     } 

    }; 
} 
+0

我有這樣的:函數SortData(){ VAR xhttp =新的XMLHttpRequest(); xhttp.onreadystatechange = function(){ if(this.readyState == 4 && this.status == 200){ } }; xhttp.open(「POST」,「Home/SortByColumn」,true); xhttp.setRequestHeader(「Content-type」,「application/x-www-form-urlencoded」); xhttp.send(「sortCriteria = FullName」); xhttp.send(); }它正在工作併發送變量,但出現此錯誤:未捕獲DOMException:未能在'XMLHttpRequest'上執行'發送':對象的狀態必須爲OPENED。在控制檯 – Alma

+0

我認爲這裏的問題是,你正在調用xhttp.send();兩次。一旦發送了數據,並且一次沒有參數。在我的例子中,我添加了一條評論,說使用xhttp.send(params);如果你不得不提交和使用xhttp.send();如果你不需要提交任何東西。不要同時使用兩個:)我會編輯我的答案 – BFG

2

當然可以。實際上,jQuery是基於javascript的庫,它不是一個獨立的語言。這裏是你必須做的:

function SortData(){ 
    // Every ajax call is an XMLHttpRequest 
    var xhttp = new XMLHttpRequest(); 

    // It means that your request is processed asynchronously. 
    // So we need to define the method that has to be run once the response is received, 
    xhttp.onreadystatechange = function() { 

    // status 200 means that your request has been processed successfully. 

    if (this.readyState == 4 && this.status == 200) { 
     // Change your html here 
    } 
    }; 
    // Setting your request 
    xhttp.open("POST", "mycontroller/myaction", true); 

    // Send your request when everything is set. 
    xhttp.send(); 
} 

如果你需要更多的瞭解,看看這個鏈接:https://www.w3schools.com/xml/ajax_intro.asp

相關問題