简体   繁体   English

在获取 API 响应时,在 javascript 文件中获取空白响应

[英]Getting blank response in the javascript file while fetching the API response

在此处输入图像描述 I have a button.我有一个按钮。 When I click that button, Login method in the js file gets executed.当我单击该按钮时,将执行 js 文件中的登录方法。 Inside the Login method, I am calling an API.在 Login 方法中,我调用的是 API。 I am trying to fetch the output returned by the API but I am getting blank response in the alert message in the below code.我正在尝试获取由 API 返回的 output 但我在以下代码的警报消息中得到空白响应。 I am getting the correct response when trying to hit the API through Postman.尝试通过 Postman 命中 API 时,我得到了正确的响应。 Here is the js code where I am calling the API-这是我调用 API 的 js 代码-

function Login() {
    
    var url="https://localhost:44369/categories?username="+"sid"+"&password="+"123";
   

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
   
        alert(xhr.responseText);
    
  }
  xhr.open('GET', url, true);
  xhr.send(null);
}

Here is the get API code-这是获取 API 代码-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace login.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        [HttpGet]
        [Route("categories")]
        public IHttpActionResult Get(string username,string password)
        {
            loginResponse response = new loginResponse();
            if(username=="sid" && password== "123"){

                response.Success = true;
            }
            else
            {
                response.Success = false;
            }
            return Ok(response);

        }

       
        // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/values
        public void Post([FromBody] string value)
        {
        }

        // PUT api/values/5
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }
}

This worked for me -这对我有用 -

var username=document.getElementById("UserName").value;
var password=document.getElementById("Password").value;
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://localhost:44369/categories?username=sid&password=12', true);
xhr.send();
xhr.onreadystatechange = () => {
    console.log(xhr);
    if (xhr.readyState=== 4) {
        console.log(xhr.response);
    }
}

If you confused you can create code for JavaScript XHR in Postman, follow below steps:如果您感到困惑,您可以在 Postman 中为 JavaScript XHR 创建代码,请按照以下步骤操作:

在此处输入图像描述

在此处输入图像描述

I think below code works:我认为下面的代码有效:


var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function() {
  if(this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("GET", "https://localhost:44369/categories?username=sid&password=123");

xhr.send();

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

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