[英]Send Custom HTTP Body with AJAX POST request
如何使用純Javascript(而非JQuery)通過POST AJAX請求發送自定義HTTP正文? 我實際上正在嘗試在體內發送JSON文件。 我可以設置自定義標頭字段,但找不到如何設置HTTP正文。
下面是代碼
function calculateorder() {
document.getElementById("finalize").style.display = "inline";
url1 = "https://ethor-prod.apigee.net/v1/stores/";
url2 = "/orders/calculate?apikey=wSgbv9PE8aJhDOI17vvTUX1NlAceUXG7";
url = url1 + store_id + url2;
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}
}
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.send(JSON.stringify(calculate));
}
當我將相同的標頭和JSON文件與Rested(OSX HTTP客戶端)一起使用時,它可以完美地工作
在XmlHttpRequest
Obeject的.send()
方法中添加參數
像這樣:
xhr.send('username=me');
發送JSON格式數據myData
如下所示:
xhr.send(JSON.stringify(myData));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.