[英]Post Request to Restful Web service using javascript
我試圖為我的Web服務創建一個客戶端,因此我試圖使用Javascript向Web服務發出POST請求,但沒有得到任何響應。 請向我建議對我的Web服務進行發布請求的正確方法。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>AIMS LoginPage</title>
<script type="text/javascript">
function validate(){
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest;
}
else{
xmlhttp=new ActiveObject('Microsoft.XMLHTTP');
}
if(xmlhttp)
{
var username=document.index.Uid.value;
var password=document.index.pass.value;
var a="<?xml version=\"1.0\" encoding=\"UTF-8\"?><credentials><username>";
var b="</username><password>";
var c="</password></credentials>";
var authDetails=a+username+b+password+c;
document.getElementById("h").innerHTML=`${authDetails}`;
xhttp.open("POST", "webserviceURIhere", true);
xhttp.setRequestHeader("Content-type", "text/xml");
xhttp.send(authDetails);
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("h").innerHTML = this.responseText;
}
};
}
}
</script>
</head>
<body>
<form name="index" onsubmit="return validateForm();" method="POST">
<tr>
<td>UserName </td>
<td><input type="text" name="Uid" id="Uid" placeholder="Enter Username"></td>
</tr><br>
<tr>
<td>Password </td>
<td><input type="password" name="pass" id="pass" placeholder="Enter Password"></td>
</tr><br>
<tr>
<td>
<input type="button" value="Login" name="submit" onclick="validate()"></td>
</tr>
</form>
<p id="h"></p>
</body>
</html>
您正在傳遞字符串'authDetails'
而不是變量authDetails
。
編輯:
//確保發送正確的標頭信息以及請求
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.