在JavaScript中使用 '的XMLHttpRequest' 會。看看here。
例子:
var objectToSend = {
id: "1",
string: "iamnewtojavascript"
};
var request = new XMLHttpRequest();
// callback function for readyState
request.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200) // check if request has completed, and check if server response is valid
alert(this.responseText) // alert received anwser from server
}
// open connection to server
request.open("POST", /* URL GOES HERE */, true);
// send "objectToSend"
request.send(JSON.stringify(objectToSend));
這就是如何做一個POST請求到服務器。 HTTP-Post-Request用於在服務器上創建數據。
但我認爲你想使用的是一個HTTP-Get-Request。爲了做到這一點,只需將request.open("POST", ...)
更改爲request.open("GET", 'url', true);
並且不需要參數即可撥打request.send()
。
有關HTTP-Post-Request和HTTP-Get-Request之間差異的說明,請參閱this。
你是什麼意思沒有結果給你一個滿意的答案?你已經嘗試過任何東西,例如對該端點的簡單Ajax調用? – adeneo
我是非常新的JS和大多數的結果是使用JSON.stringify,從我的理解是讓你張貼數據手動JSON。我的API使用URL中的變量,並且要做的是檢索。該框架正在完成其餘的代碼。 – ottomd
您的API需要返回JSON。據我瞭解你當前的代碼,你只是將JSON打印到服務器控制檯,但如果有人向您的API發送POST請求,則不會返回任何內容。 [首先,如果有人發送POST請求,你必須閱讀如何返回內容。](http://docs.python-requests.org/en/latest/user/quickstart/) 在你理解了您可以使用[XMLHttpRequest API發送POST請求](https://developer.mozilla.org/de/docs/Web/API/XMLHttpRequest)。響應對象將包含API中的JSON。 – Patte