app.post在启动提取时没有收到任何响应

时间:2017-11-25 09:56:55

标签: javascript node.js

所以我想将一个文件发送到节点服务器,这需要前端的东西。我环顾四周,看看如何使用我的app.post("",req)来检索网站的确认。但是我遇到了一个问题。

以下是示例代码:



app.use(bodyParser.json())

app.post("/resources",(req, resp)=>{
	console.log(req.body)
	resp.json(jsnfile) // this part works however
})




这是前端,现在是后端(服务器端)......



import { addComment } from '../../store/actions'
export default {
  computed: {
    addComment
  },
  vuex: {
    actions: { addComment }
  },
  data () {...},
  methods: {
    sendComment: function () {
        this.addComment({
            commentable_id: this.id,
            commentable_type: this.model,
            content: this.content,
            reply: this.reply
        })
    }




当这两个一起运行时,前端的javascript不会在后端收到,它返回一个空字典{}。我做错了什么?

1 个答案:

答案 0 :(得分:0)

我似乎并不是在发送您fetch()请求的文件。试试这个:

var input = document.querySelector('input[type="file"]');

# If you gonna take a file from the input
var data = new FormData();
data.append('file', input.files[0]);

# Asuming 'slink' is a valid url
fetch(slink, {
  method: 'POST',
  body: data
});

this issue可能重复。

修改

如果您想发送JSON对象,只需将此行data.append('file', input.files[0]);替换为此类data.append( "json", JSON.stringify(data));,然后像以前一样发送。