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