supuwoerc
V2EX  ›  Node.js

vue 小 demo 的 post 请求被挂起怎么办?

  •  
  •   supuwoerc · Feb 20, 2019 · 3864 views
    This topic created in 2646 days ago, the information mentioned may be changed or developed.
    如题 自己写的烂代码,之前一直没问题,最近打开想加一点功能发现跑都跑不起来了,在网页的 Network 看见请求被挂起:Provisional headers are shown,之前遇到过一次过会不知道怎么就好了,没怎么在意,网上看见的解决方法都不好使,因为是本地项目,都是 http 请求,不是网上说的安全降级问题,而且不只是谷歌浏览器,所以不是浏览器问题,这次想不到原因的再次出现,问问大家这怎么解决。
    也不知道怎么了,貌似是对象传过去解析不出来的原因,但是之前还能跑,咋回事啊。。。

    注:前端的提交代码
    ` this.$http.post('saveuser',
    {
    uname: this.$refs.uname.value,
    upassword: this.$refs.upassword.value,
    email: this.$refs.email.value,
    problem: this.$refs.problem.value,
    answer: this.$refs.answer.value
    }).then(function (res) {
    if (res.body.code == 1) {
    this.registerOk = true;
    this.registerMsg = "注册成功";
    setTimeout(function () {
    this.$router.push({path: '/index'});
    }.bind(this), 1500);

    } else {
    this.registerOk = true;
    this.registerMsg = res.body.msg;
    }
    }.bind(this))`


    注:后端的 node 代码
    `exports.saveUser = function (req, res) {
    console.log("这里是 saveuser 方法");
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
    if (fields.email) {
    user.find({
    email: fields.email
    }, function (err, data) {
    // 新用户数据存入数据库
    if (data.length == 0) {
    user.insertUser(fields, function (result) {
    console.log("数据处理完成,code=" + result);
    res.json({
    "code": result
    });
    });
    }
    //数据库查出来存在这个用户了 返回已经注册的信息
    else {
    res.json({msg: "该邮箱已注册"});
    }
    });
    }
    });
    };`
    Supplement 1  ·  Feb 21, 2019
    问题已经解决辣,是请求头的问题,form-data 类型才行,选择 new FormData 川个对象就能正确返回了,请求被挂起的原因是等待响应,一直等到超时。
    qilishasha
        1
    qilishasha  
       Feb 21, 2019
    setTimeout 中不用箭头,现在直接可以用 this 了吗??
    supuwoerc
        2
    supuwoerc  
    OP
       Feb 21, 2019
    @qilishasha 额,我用 bind 修改了 this 指向啊。。 而且和箭头函数没关系啊,箭头函数也可以修改指向的啊。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1480 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 17:00 · PVG 01:00 · LAX 10:00 · JFK 13:00
    ♥ Do have faith in what you're doing.