如何从角度服务返回xmlhttp请求的响应?

时间:2019-05-06 05:55:23

标签: javascript ajax angular xmlhttprequest

我尝试以角度显示服务中xml httprequest的响应。但是它返回未定义的。我也尝试使用提取API。它返回相同的

    getResponse() {
        let response
        var xhttp = new XMLHttpRequest();
        xhttp.open("GET", "https://www.api-endpoint.com/get/heros.html", true);
        xhttp.send()

        xhttp.onreadystatechange = function () {
          if (this.readyState == 4 && this.status == 200) {
            response = this.response
          }
        }

    return response 
      }

getResponse(){
    let url="https://www.api-endpoint.com/get/heros.html"
    let response
    fetch(url)
    .then(res=>JSON.stringify(res))
    .then(data=> response=data)

    return response
}

0 个答案:

没有答案