2016-07-22 33 views
0

測試koa.js並在客戶端獲取。我正試圖在服務器上發佈一些數據。我正在使用下面的代碼未定義。我不確定爲什麼。Koa路由器沒有看到帖子正文?

我已經檢查thisthis.requestthis.req,等我不能從客戶端查找REQ我的數據?

服務器

import koaRouter from 'koa-router' 

// Post route 
router.post('/api/upload', function *(next) { 
    console.log('UPLOAD WORKS', this.req.body) 
    this.status = 200 
}) 

app 
    .use(bodyParser()) 
    .use(router.routes()) 
    .use(router.allowedMethods()) 

控制檯給我不確定。

客戶

fetch('/api/upload', { 
    method: 'post', 
    body: 'Hello' 
}).then(function(response) { 
    console.log(response) 
}).catch(function(err) { 
    // Error :(
}); 
+0

可以複製的[這](http://stackoverflow.com/questions/31504814/koa-router-and-post) –

+0

我跨過來了。我仍然錯過了我的數據。我想它可能是因爲fetch()。 –

+0

如果你發現了一些關於你的問題的文章,請在文中加入這些文章,並告訴你爲什麼不是你的問題的答案。 –

回答

0

你應該改變你的客戶混帳使用「FORMDATA」對象包裝的字符串。

var formData = new FormData(); 
formData.append('json', 'Hello'); 

fetch('/api/upload', { 
    method: 'post', 
    body: formData 
}).then(function(response) { 
    console.log(response) 
}).catch(function(err) { 
    // Error :(
});