0
我有jQuery的AJAX查询:
var data2 = "data"
var ajaxOptions2 = {
type: "post",
url: "http://localhost:8050/adapter/interface",
data: data2,
dataType: "json",
success: onSuccess,
error: onError,
processData: false,
contentType: "application/json; charset=UTF-8"
};
$.ajax(ajaxOptions2);
和这个职位要求工作正常
我试图改写这一对angular2 http.post:
let bodyString = "data";
let headers = new Headers({ 'dataType': 'json', 'contentType': 'application/json; charset=UTF-8' });
let options = new RequestOptions({ headers: headers, method: RequestMethod.Post});
that.http.post(that.url, bodyString, options)
.map(that.extractData)
.catch(that.handleError)
.subscribe(data => { console.log("test")});
但http.post将返回错误:
XMLHttpRequest cannot load http://localhost:8050/adapter/interface. Request header field dataType is not allowed by Access-Control-Allow-Headers in preflight response.
你能告诉为什么为什么jquery ajax工作,但我的http.post不?
这篇文章可以帮助你:https://stackoverflow.com/a/25727411/6053654 –
他们显然不一样,如果一个在工作,另一个不是。 jQuery的'dataType'不是一个头文件,它只是告诉jQuery将返回的字符串解析为JSON,它永远不会发送到服务器。 – adeneo