node.js - 问个问题 Uncaught (in promise)
大家讲道理
大家讲道理 2017-04-17 16:16:10
0
2
306

是这个样子的
vue+vue-resource+express组合
然后在下面这里遇到问题了

client

this.$http.jsonp('http://localhost:3300/register', { params: { name: this.name, password: this.password, repassword: this.repassword } }, {})
          .then(function (response) {
            console.log(response.data.state)
          })

server

app.get('/register', function (req, res) {     
            userTools.create(user).then(function(result){
                res.jsonp(result)
            }).catch(function(err){
                res.jsonp(data)
            })
    })

这样就会出错

如果server改为下面这样就不出包错了

app.get('/register', function (req, res) {     
     res.jsonp(data)
     }

这是问什么啊?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
伊谢尔伦

First of all, I agree with the opinion above, I also think it is an error reported by the server

Judging from the error picture, the first error is due to the jsonp request initiated by the poster, but the response header set when returning is set to 'application/json'. The poster can understand the principle of jsonp and try to call res.setHeaders in get. (It seems that I can’t remember this API clearly. In short, it just sets the response header). Try changing the mine type of the response data to 'application/javascript'

Secondly, the Uncaught (in promise) error refers to an error when calling promise. It is a subsequent error caused by the first error, but the client does not catch it. The author can write like this

this.$http.jsonp('http://localhost:3300/register', { params: { name: this.name, password: this.password, repassword: this.repassword } }, {})
          .then(function (response) {
            console.log(response.data.state)
          }).catch(e => {
              // 打印一下错误
              console.log(e)
          })
伊谢尔伦

There should be a problem with the sever code. Check to see if there is an error in the server part

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!