如何从XMLHTTP请求返回自定义响应?

时间:2019-06-14 03:10:11

标签: javascript ajax api xmlhttprequest

从股票API中提取数据,获取所有交易品种名称。我正在从HTML输入中获取一个字符串符号,并使用该值来匹配从API中提取的符号。

我正在寻找返回一个简单的布尔值(如果已找到),它已添加到request.onload方法中。当我从其他方法调用/获取响应时,会看到对象的未定义或文本响应。

我在按钮单击侦听器中添加了一个“加载”事件侦听器,我尝试在request.send方法内部添加了布尔变量,我添加了一个就绪状态更改侦听器,似乎有帮助,但没有响应类型从onload方法返回布尔变量。


const searchInput = document.getElementById("symbol-search-input");
const symbolSearchBtn = document.getElementById("symbol-search-btn");
const symbolSearchMsg = document.getElementById("symbol-status-msg");
const request = new XMLHttpRequest();

symbolSearchBtn.addEventListener("click", () => {

    //Value from html input
    let symbolSearch = searchInput.value;

    if(symbolSearch == "") {
        symbolSearchMsg.innerHTML = "symbol field blank";

    } else {
        //calling XMLHTTPRequest to begin the process
        symbolFound();

        request.onreadystatechange = () => {
            if(request.readyState == 4) {

                if(request.status == 200) {
                    //Below it only returns the entire JSON as text even if I only use "request.response"
                    console.log(request.responseText);

                }

                if(request.status == 404) {
                    console.log('File or resource not found');
                }

            }
        };


let symbolFound = () => {

    request.open('GET', 'https://api.iextrading.com/1.0/ref-data/symbols', true);
    let foundSymbol = false;

    request.onload = function() {
        let data = JSON.parse(this.response);

        if(request.status >= 200 && request.status < 400) {
            let symbolInput = searchInput.value.toUpperCase();

            for(let i = 0; i < data.length; i++) {
                if(data[i]['symbol'] === symbolInput) {
                    foundSymbol = true;
                    break;
                }
            }
            return foundSymbol;

        } else {
            console.log("error");
        }
    };

    request.send();
};


我希望收到布尔值返回(true或false),相反,我从API接收到的JSON是文本/字符串。

1 个答案:

答案 0 :(得分:0)

我想出了办法,至少是一种解决方法。不用返回任何值,我只是让它更改了变量的状态,然后调用了另一个函数来执行我需要的布尔值。解剖初始功能也是一个问题。看看。

const searchInput = document.getElementById("symbol-search-input");
const symbolSearchBtn = document.getElementById("symbol-search-btn");
const symbolSearchMsg = document.getElementById("symbol-status-msg");
let isSymbolFound;


symbolSearchBtn.addEventListener("click", () => {

    let symbolSearch = searchInput.value;

    if(symbolSearch == "") {
        symbolSearchMsg.innerHTML = "symbol field blank";

    } else {
        isSymbolFound = false;
        const xhr = new XMLHttpRequest();
        xhrProcess(xhr);

        xhr.onload = function() {
            let data = JSON.parse(this.response);

            if(xhr.status >= 200 && xhr.status < 400) {
                let symbolInput = symbolSearch.toUpperCase();

                for(let i = 0; i < data.length; i++) {
                    if(data[i]['symbol'] === symbolInput) {
                        isSymbolFound = true;
                        break;
                    }
                }
                symbolFindProcess();

            } else {
                console.log("error");
            }
        };
    }
});

const symbolFindProcess = () => {
    if(isSymbolFound) {
        //code here
    } else {
        //code here
    }
};

const xhrProcess = (request) => {
    request.open('GET', 'https://api.iextrading.com/1.0/ref-data/symbols', true);
    request.send();
};