2013-07-22 84 views
0

中調用其他網絡服務我是新增的web服務和html/javascript。 我已經用java編寫了一個REST Web服務。它接受來自html表單的用戶名並再次將其返回到相同的html頁面。我使用alert()函數調用顯示哪些內容。無法通過html/javascript從參數

我的web服務代碼是

@Path("/hello") 
public class Hello { 

@GET 
    @Path("hello/{username}") 
    @Produces(MediaType.TEXT_HTML) 
    public String ldap(@PathParam("username") String username) 
    { 
    return username; 
    } 
} 

我的JSP文件first.jsp(粘貼所選擇的代碼而已,所以沒有關於錯誤的html,body標籤)

<script type="text/javascript"> 
var xmlhttp; 
function callservice() 
{ 
     alert("I am in javascript"); 
     var user = document.getElementById("uid"); 
     xmlhttp = new XMLHttpRequest(); 
     var url = "http://localhost:8080/de.vogella.workspace.first/rest/hello/" + user.value; 
     xmlhttp.open('GET', url, true); 
     alert("after url"); 
     xmlhttp.send(null); 
     alert("after send"); 
     xmlhttp.onreadystatechange = function(){ 
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
       { 
       alert("In functn"); 
       alert(xmlhttp.responseText); 
       } 

     } 

    } 
</script> 

    <form style="margin-left:300px" id="f1" method="GET" onsubmit="callservice()" > 
      <fieldset style="margin: 50px"> 
      <legend>Login</legend> 
       <p> 
       User name: <input type="text" id="uid" name="username"><br> 
       <p> 

       <input type="submit" value="Submit"> 
       <p id="qwe"> This HTML</p> 
      </fieldset> 
     </form> 

從第一。我已經調用了Web服務的jsp文件。我在沒有任何參數的情況下做同樣的事情,我從web服務返回一些硬編碼值,然後運行。但我需要傳遞的參數,因爲我想認證用戶。

我想我在參數傳遞中做錯了什麼。

當我點擊提交按鈕,我可以看到在瀏覽器URL作爲

http://xxx.xxx.xxx.x:8080/de.vogella.workspace.first/first.jsp?username=john 

,當我測試Web服務作爲獨立的,它看起來像

http://xxx.xxx.xxx.x:8080/de.vogella.workspace.first/rest/hello

任何人可以請幫助我來整理一下。

謝謝。

回答

0

您的問題是JavaScript正在運行一個ajax調用,並且完成表單提交時。完全刪除表單,只需一個按鈕和輸入框即可調用您的javascript。如果可能的話,你還應該考慮使用jquery。

看看這個鏈接瞭解你的問題。 How to prevent form from being submitted?

此外,調試技巧使用螢火蟲net面板來查看實際上從您的代碼生成的請求。