【发布时间】:2017-03-09 04:45:52
【问题描述】:
我在向我的 express 应用发出 POST 请求时遇到了一个奇怪的问题。
我已经使用 Postman 测试了 API,但是当我从 Postman 复制 AJAX 或 XHR 的请求代码时,请求失败并且快递应用返回未定义的快递正文。
网站上的控制台显示以下内容:
Resource interpreted as Document but transferred with MIME type application/json
请求看起来像这样(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) {
console.log(response);
});
为什么这可以在 Postman 和使用 Curl 的控制台中工作,而不是来自 Web 文档?
【问题讨论】:
-
我也启用了 Cors,所以应该不是问题。
-
正如这个答案所暗示的,将响应类型设置为 text/html:stackoverflow.com/a/7053252/1373554
-
你会如何在快递应用@gjegadesh 中做到这一点?
-
您的内容类型值与您的数据不匹配。几乎从不需要跨域。 async 的默认值为 true。别再往墙上扔意大利面了。
标签: javascript jquery ajax node.js