V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
supuwoerc
V2EX  ›  Node.js

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

  •  
  •   supuwoerc · 2019-02-20 21:29:39 +08:00 · 3273 次点击
    这是一个创建于 1863 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如题 自己写的烂代码,之前一直没问题,最近打开想加一点功能发现跑都跑不起来了,在网页的 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: "该邮箱已注册"});
    }
    });
    }
    });
    };`
    第 1 条附言  ·  2019-02-21 18:56:23 +08:00
    问题已经解决辣,是请求头的问题,form-data 类型才行,选择 new FormData 川个对象就能正确返回了,请求被挂起的原因是等待响应,一直等到超时。
    qilishasha
        1
    qilishasha  
       2019-02-21 10:49:50 +08:00
    setTimeout 中不用箭头,现在直接可以用 this 了吗??
    supuwoerc
        2
    supuwoerc  
    OP
       2019-02-21 18:57:46 +08:00
    @qilishasha 额,我用 bind 修改了 this 指向啊。。 而且和箭头函数没关系啊,箭头函数也可以修改指向的啊。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2493 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:02 · PVG 00:02 · LAX 09:02 · JFK 12:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.