weixin_33725807 2016-10-26 17:34 采纳率: 0%
浏览 52

Express App发布请求

I am having an odd issue making a POST request to my express app.

I have tested the API using Postman however when I copy the request code from Postman either the AJAX or XHR the request fails and the express app returns an undefined express body.

The console on the site spits out the following:

Resource interpreted as Document but transferred with MIME type application/json

The request looks like this (AJAX):

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://thedomainhere.com/hello",
  "method": "POST",
  "headers": {
    "content-type": "application/json",
    "cache-control": "no-cache",
    "postman-token": "0158785a-7ff5-f6a3-54ba-8dfc152976fc"
  "data": {
    "senderEmail": "hello@hello.com"

$.ajax(settings).done(function (response) {

Why would this work on Postman and in the console using Curl, but not from a web document?

  • 写回答

2条回答 默认 最新

  • ℙℕℤℝ 2016-10-26 18:03

    jQuery first makes OPTIONS request without POST payload, when you try to make cross-origin ajax, and expects the following headers to be sent in response:

    Access-Control-Allow-Origin: *
    Access-Control-Allow-Methods: POST
    Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept

    To set headers on response in Express you can do the following middleware somewhere in the beginning (also see NodeJS docs for setHeader):

    app.use(function(req, res, next) {
        res.setHeader('Access-Control-Allow-Origin', '*');
        res.setHeader('Access-Control-Allow-Methods', 'POST');
        res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
        if(req.method == 'OPTIONS') {
        } else {

    Also this post about enabling CORS on express may be useful.

    To understand why OPTIONS is sent in this case, you can read an answer to this question.

    That's the first possible reason, why you get empty body.

    Also try to change content-type to contentType (as in jQuery docs) and pass a string in the data

    data: JSON.stringify({
        "senderEmail": "hello@hello.com"



  • ¥15 三菱变频器模块怎么检测触发
  • ¥15 在C#方法中使用了.Refresh();刷新了控件,但是不是最新值
  • ¥30 求一下解题思路,完全不懂。
  • ¥30 关于#硬件工程#的问题:求一下解题思路
  • ¥15 运筹学对偶单纯行法构造扩充问题
  • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗