2012-01-10 21 views
0

私は新しいWebサービスです。私は、簡単なログインアプリケーション用のJava上の単純なWebサービスのRESTを作成しようとしている...ここに私のコードです:スタンドアロンの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> 

私をwebContent url(http://localhost/TestWSProject/index.html)から私のクライアントにアクセスしてください。サービスは完全に動作しますが、スタンドアロンのHTMLファイル(file:/// D:/ + Prog/webservice/TestWSProject /WebContent/index.html)それは私にxmlHTTPStatus = 0とそのサービスは動作しません..任意のソリューション この問題??実際に感謝..

+0

同じ起源ポリシー - http://ja.wikipedia.org/wiki/Same_origin_policy – Perception

+0

ええ..これを解決する方法を知っていますか?ありがとう.. –

答えて

0

一部のブラウザでは、ファイルシステムから直接アクセスする場合、ファイルが特定の操作を実行するのを制限するセキュリティ制限があります。

これは、エラーの原因となる可能性があります。

+0

はい..私はあなたに同意する..しかし私の場合、私は他のスタンドアロンのファイルからいくつかのWebサービスを呼び出す必要があります..任意のアイデア??ありがとう.. –

+0

このトピックに関するこのトピックに関するいくつかの興味深い投稿があります(http://stackoverflow.com/questions/4208530/xmlhttprequest-origin-null-is-not-allowed-access-control-access-allow-for-ファイル)。これはあなたを正しい方向に向けるでしょう。 – DaveE

+0

あなたのコメントdaveE ..ありがとうございます。私はGoogle Chromeを使用すると、そのリンクはインラインで動作しますが、この場合、私はモバイルデバイスブラウザを使用してWebサービスを取得します..他の提案?本当にここにはまった.. T.T –

関連する問題