我正在使用角度Http在角度项目中发出http post请求,我得到了以下错误 .
请求 Headers 字段预检响应中的Access-Control-Allow-Headers不允许Content-Type .
但是如果我用jquery发出那个帖子请求,我不会得到任何错误,它会按照需要的方式执行 .
这是我正在使用的jquery代码,它没有任何问题 .
$(function () {
$.post(
"http://www.anything.com/pdfGenerator/",
{
orientation: "portrait",
paperSize : "letter",
return_url : "true",
htmlContent: `<h1>Hello</h1>`
}
).done(function (data) {
alert("Data Loaded: " + data);
});
});
这是Angular代码,它不起作用,我得到上面提到的错误 .
this.url = "http://www.anything.com/pdfGenerator/";
let body:any = {
orientation: "portrait",
paperSize : "letter",
htmlContent: `<h1>Hello</h1>`,
return_url : "true",
};
this.http.post(this.url, body).
subscribe(res => console.log(res));
我听说有人说服务器端的 Response-Header
中添加了Content-Type . 我的意思是,如果使用jquery工作正常,我为什么要在服务器上的响应头中添加它 . 如果它正在通过jquery而没有做任何额外的事情,那么angular也应该运行它而不显示错误 .
我做错了什么?我应该怎么做才能让它在没有修改服务器的情况下以角度运行,因为jquery已经在做了?
1 回答
可能发生的一件事是jQuery发送的不同于
Content-Type
而不是angular 's $http. That might be down to the defaults, so you can try to update it to match jQuery'(您可以检查网络请求以比较正在发送的标头并找到差异) . 在这种情况下,可能是jQuery默认为服务器所期望的Content-Type
,而angular默认为其他东西 . 要在角度上设置内容类型,您可以使用以下语法:[1]我看到通过字符串化来解决这类问题的情况,无法判断是否是特定情况,因为我无法访问您的环境,但您可以试一试:
[1] https://docs.angularjs.org/api/ng/service/ $ http#post