1
所以,我在Javascript中的下一個代碼,其中的值是在發送該URLHTTP POST PARAM在URL角2
//params = 5
function httpPostAsync(url, params){
// test: curl -X POST http://url/brightness/5
var http = new XMLHttpRequest();
http.open("POST", "/brightness/"+ params , true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
//Popup good for debugging
}
}
http.send(null);
}
我的問題是,我怎麼能做到這一點的角? 我有一個代碼,但它doesn't發送值 '5' 的URL
//服務
postValue(data) {
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });
let params = new URLSearchParams();
params.append('value', data);
return this.http.post(url, params.toString(), options)
.timeoutWith(2000);
}
//組件
let value = 5;
this._localserverService.postValue(value).subscribe();
我知道,當我發送帶有表單urlencoded內容類型的數據,我應該使用key = value對以查詢字符串格式發送數據。但是這在我的舊代碼中並不需要。
我也知道http.post的第二個參數是消息體(有效負載),而不是URL搜索參數。
我應該做什麼不同?
謝謝!
我不知道這是可能的。謝謝! – sie