使用javascript向Restful Web服务发布请求

时间:2018-05-13 01:47:41

标签: javascript jquery html rest jsp

我正在尝试为我的Web服务创建一个客户端,因此我尝试使用Javascript向Web服务发出POST请求,但我没有得到任何响应。请建议我使用我的网络服务发布请求的正确方法。

<!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>

0 个答案:

没有答案
相关问题