简体   繁体   English

REST API的XHTTP请求

[英]XHTTP request from REST API

I have this API 我有这个API

[HttpGet("data")]
public dynamic GetData(){
    return context.DataTable.ToList();
}

I tried calling it on my Javascript using this snippet; 我尝试使用此代码段在Javascript上调用它;

function getData(){
    var xhttp = XMLHttpRequest();
    xhttp.open("GET", "api/myclass/data", true);
    xhttp.setRequestHeader("Content-type","application/json");
    xhttp.send();
    var resp = xhttp.responseText;
}

However, it only returns empty XMLHttpRequest . 但是,它仅返回空的XMLHttpRequest

I think what's wrong there is the URL. 我认为URL出了问题。 How I may able to call the API to my Javascript? 我怎么能调用我的Javascript API?

The request may take time to receive the response so you have to wait. 该请求可能需要一些时间才能收到响应,因此您必须等待。 Something like this. 这样的事情。

function getData(){
    var xhttp = XMLHttpRequest();
    xhttp.open("GET", "api/myclass/data", true); //the request is asynchronous
    xhttp.onreadystatechange = function(){
       if(this.readyState == 4 && this.state == 200){ //**this** is xhttp
          //data are received and ready to use
          var resp = this.responseText;
          //do whatever you want with resp but never try to **return** it from the function
       }
    }
    xhttp.setRequestHeader("Content-type","application/json");
    xhttp.send();
    //var resp = xhttp.responseText; //too early ;(
}

Since u have not cheked the response of ur answer, i susspect there is something wrong in ur backend. 由于您尚未检查您回答的答复,因此我怀疑您的后端出了点问题。 But, here is a sample of functional solution: 但是,这是功能解决方案的示例:

 <!DOCTYPE html> <html> <body> <h2>Using the XMLHttpRequest Object</h2> <div id="demo"> <button type="button" onclick="loadXMLDoc()">Change Content</button> </div> <script> function loadXMLDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { console.log("Status is: "+this.status); if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = this.responseText; } }; xhttp.open("GET", "xmlhttp_info.txt", true); xhttp.send(); } </script> </body> </html> 

You van find more info here . 您可以在此处找到更多信息。 But in the line 但是在行

xhttp.open("GET", "api/myclass/data", true);

The second parameter is the address of a file in ur server. 第二个参数是ur服务器中文件的地址。 ru sure u have wrotten the correct format? 您确定您输入了正确的格式吗? what is the extension of ur data file. ur data文件的扩展名是什么。

I guess, both backend and front end should be reconsidered. 我想,后端和前端都应该重新考虑。 To do it: 去做吧:

  1. Try to send a reuqest using postman to backend 尝试使用邮递员发送请求到后端
  2. in frontend check the status of response using my answer 在前端使用我的答案检查响应状态
  3. To make sure make it async = false with 为了确保使其与async = false

    xhttp.open("GET", "api/myclass/data", false); xhttp.open(“ GET”,“ api / myclass / data”,false);

Therefore, there wouldn't be a delay as @Alex Kudryashev pointed 因此,@ Alex Kudryashev指出,不会有延迟


Solution: 解:

You need to first find the result of line 您需要先找到line的结果

console.log("Status is: "+this.status);

in ur browser's console. 在您的浏览器控制台中。

If u get the responseText as empty it may come because u have sent an empty string from backend,(we are not sure because u have not tested ur backend with postman) but it is crucial to know the status of response. 如果您将responseText设为空,则可能是因为您从后端发送了一个空字符串(我们不确定,因为您尚未使用邮递员测试您的后端), 但是了解响应的状态至关重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM