2017-02-23 89 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

相关问题