weixin_33737774 2015-11-29 18:27 采纳率: 0%
浏览 814

解析xhr.responseText

I'm having a hell of a time trying to use xhr.responseText to verify whether a form entry already exists in a mongo database. I'm creating a POST request, and am setting this.body to an easily-parseable string "ERROR" or "SUCCESS".

However, I'm not able to use the xhr.responseText in any way that I would normally be able to use a string, even though typeof shows that it's a string

Below is my api.js which is handling the request. I'm setting the xhr.responseText by setting this.body to the relevant String.

router.post('/edit', function *(req, res, next) {
    console.log(this.request.body);

    let {url, links} = this.request.body

    let obj = yield linkrSchema.findOne({urlExt: url})

    //if that entry already exists
    if (obj !== null){
        this.body = "ERROR";
    }
    else{
        this.body = "SUCCESS";
        linkrSchema.createLinkr(url, 'testuser', links, function(err, linkr){
            console.log(err, linkr);
        });
    }
})

Below is the relevant part of my Edit.js, where the POST request is made.

    else{
        var xhr = new XMLHttpRequest();   // new HttpRequest instance 
        xhr.open("POST", "/edit");
        xhr.addEventListener("load", e => {
            console.log(xhr.responseText)
        });

        xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        console.log(xhr.responseText === "ERROR"); //false
        console.log(xhr.responseText);             //ERROR
        var res = xhr.responseText.match(/ERROR/); //null

        alert(res)
        if (xhr.responseText === "ERROR") {        //never gets here :(
            alert(xhr.responseText);
        }
        else{

            xhr.send(JSON.stringify(this.state));
            console.log(this.state)
            window.location.href = "/" + this.state.url;
        }

    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?