2012-01-10 72 views
0

我是一個web服務新手。我試圖做出一個簡單的Java Web服務休息一個簡單的登錄應用程序。這裏是我的代碼:如何從獨立的html文件訪問Web服務?

服務器端:

package com.testingws.webservices; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 


@Path("/login/{username}/{password}/{datetime}") 
public class webServicesClass { 
    @GET // this method process GET request from client 
    @Produces("application/json") // sends JSON 
    public String getJson(@PathParam("username") String username, @PathParam("password") String password) { // empno represents the empno sent from client 
     if (username.equalsIgnoreCase("admin") && password.equalsIgnoreCase("admin")){ 
      return "{'loginstatus':'success'}"; 
     } 
     else{ 
      return "{'loginstatus':'failed'}"; 
     } 
    } // end of 

} 

客戶端:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Client Login</title> 
<script type="text/javascript"> 
    function loginProcess(){ 
     var tempUser = document.getElementById("loginUsername"); 
     var tempPass = document.getElementById("loginPassword"); 

     var dateTime = new Date(); 
     var url = "http://localhost:8181/TestWSProject/authentication/login/" + tempUser.value + "/" + tempPass.value + "/" + dateTime.toUTCString(); 

     var xmlhttp = new XMLHttpRequest(); //@slaks: i put it here 

     xmlhttp.open('GET',url,true); 
     xmlhttp.send(null); 
     xmlhttp.onreadystatechange = function() { 
       if (xmlhttp.readyState == 4) { 
        if (xmlhttp.status == 200) { 
         var det = eval("(" + xmlhttp.responseText + ")"); 

         //alert(det.loginstatus); 
         if(det.loginstatus=="success") 
         { 
          setCookie("login", "yes", 1); 
          window.location="main.html"; 
         } 
         else 
         { 
          alert("incorrect username or password"); 
         } 

       } 
       else 
         alert("Error ->" + xmlhttp.status + xmlhttp.responseText); 
       } 
     } 
    } 

    function getCookie(c_name) 
    { 
     var i,x,y,ARRcookies=document.cookie.split(";"); 
     for (i=0;i<ARRcookies.length;i++) 
     { 
     x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); 
     y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); 
     x=x.replace(/^\s+|\s+$/g,""); 
     if (x==c_name) 
     { 
      return unescape(y); 
     } 
     } 
    } 

    function setCookie(c_name,value,exdays) 
    { 
     var exdate=new Date(); 
     exdate.setDate(exdate.getDate() + exdays); 
     var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); 
     document.cookie=c_name + "=" + c_value; 
    } 

    function checkCookie() 
    { 
     var loginStatus=getCookie("login"); 
     //alert(loginStatus); 
     if (loginStatus=="yes") 
     { 
      //alert("Masuk pengecekan") 
      window.location="main.html"; 
     } 
    } 
</script> 
</head> 
<body onload="checkCookie()"> 
    <h2>LOGIN FORM</h2> 
    <BR> 
    Username : <input type="text" id="loginUsername"/> 
    <BR> 
    Password : <input type="password" id="loginPassword"/> 
    <BR> 
    <input type="button" value="Login" onclick="loginProcess()"/> 
</body> 
</html> 

當我從web內容url(http://localhost/TestWSProject/index.html)訪問我的客戶端,該服務完美地工作,但是如果我從獨立的HTML文件訪問我的客戶端(file:/// D:/ + Prog/webservice/TestWSProject /WebContent/index.html)它給我xmlHTTPStatus = 0,該服務不工作..任何解決方案 這個問題??非常感謝..

+0

相同來源政策 - http://en.wikipedia.org/wiki/Same_origin_policy。 – Perception 2012-01-10 02:07:24

+0

是的..你知道如何解決這個問題嗎?謝謝.. – 2012-01-10 02:25:41

回答

0

某些瀏覽器具有安全限制,如果文件系統正在直接訪問文件,則會限制文件執行某些操作。

這可能是導致錯誤的原因。

+0

是的..我同意你..但在我的情況下,我需要從其他獨立文件調用一些Web服務..任何想法?謝謝.. – 2012-01-10 02:07:26

+0

有一些關於這個問題的這個主題有趣的職位(http://stackoverflow.com/questions/4208530/xmlhttprequest-origin-null-is-not-allowed-access-control-access-allow-for-文件)。這可能會指向你正確的方向。 – DaveE 2012-01-10 02:11:47

+0

感謝您的評論daveE ..當我使用谷歌瀏覽器時鏈接功能正常,但在這種情況下,我使用移動設備瀏覽器來檢索Web服務..任何其他建議?真的卡在這裏.. T.T – 2012-01-10 02:26:47