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:首先,同意樓上觀點,我也認為是服務端報錯了
從報錯圖片第一個錯誤來看是因為樓主發起的jsonp請求,但是返回時設置的響應頭設置了’application/json’,樓主可以去了解下jsonp原理,試著在get里面調用res.setHeaders(貌似是這個api記不太清了,總之就是設置響應的header頭),把響應數據的mine類型改成’application/javascript’試試
其次 Uncaught (in promise) 錯誤是指調用promise時報錯,是由于第一條錯誤引發的后續錯誤,但是客戶端沒有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那塊是不是有報錯
相關文章:
1. bootstrp是col-md-12列的,只有col-md-10有內容,可以讓沒有內容的不占據位置嗎;2. android - Genymotion 微信閃退 not find plugin.location_google.GoogleProxyUI3. wordpress里,這樣的目錄列表是屬于小工具還是啥?4. python - Fiddler+Android模擬器抓取app,json數據被加密了,如何解析?5. python如何設置一個隨著系統時間變化的動態變量?6. 我的怎么不顯示啊,話說有沒有QQ群什么的7. mysql federated引擎無法開啟8. MySQL 使用 group by 之后然后 IFNULL(COUNT(*),0) 為什么還是會獲得 null9. 常量在外面不加引號會報錯。10. sublime text3安裝package control失敗
