[英]Post Request to Restful Web service using javascript
I am trying to make a client for my web service thus I am trying to make a POST request to the web service using Javascript but I am not getting any response. 我试图为我的Web服务创建一个客户端,因此我试图使用Javascript向Web服务发出POST请求,但没有得到任何响应。 Please suggest me the right way to do the post request to my web service.
请向我建议对我的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>
You are passing the string 'authDetails'
instead of the variable authDetails
. 您正在传递字符串
'authDetails'
而不是变量authDetails
。
EDIT: 编辑:
// Make sure you send the proper header information along with the request //确保发送正确的标头信息以及请求
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.