node.js - 問個問題 Uncaught (in promise)
問題描述
是這個樣子的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) }
這是問什么啊?
問題解答
回答1:首先,同意樓上觀點,我也認為是服務端報錯了
從報錯圖片第一個錯誤來看是因為樓主發(fā)起的jsonp請求,但是返回時設(shè)置的響應頭設(shè)置了’application/json’,樓主可以去了解下jsonp原理,試著在get里面調(diào)用res.setHeaders(貌似是這個api記不太清了,總之就是設(shè)置響應的header頭),把響應數(shù)據(jù)的mine類型改成’application/javascript’試試
其次 Uncaught (in promise) 錯誤是指調(diào)用promise時報錯,是由于第一條錯誤引發(fā)的后續(xù)錯誤,但是客戶端沒有catch住,樓主可以這么寫
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) })回答2:
應該是sever代碼有問題吧,看下server那塊是不是有報錯
相關(guān)文章:
