2015-01-01 4 views
-1

나는 웹 기술에 새로운 문제가 있습니다. 나는 PHP 서버에서 클라이언트 test.html로 데이터를 보내고 싶어한다. 하지만 경고 ("1") 만 나오지만 경고 ("2")는 인쇄되지 않습니다. 경고 (JSON.stringify (응답))에 Json 데이터를 인쇄하려고합니다., 내가 틀린 곳을 말해 주시겠습니까?PHP 서버에서 클라이언트로 데이터를 보내는 방법

내 서버 코드는 데이터베이스가 데모입니다

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>JSON </title> 
     <script src="Scripts/jquery-1.8.2.js"> 
     </script> 
     <script> 
      $(document).ready(function(){ 
       alert("1"); 
       $("#btnLogin").click(function(){ 
        alert("2"); 
        $.ajax({ 
         url:"http://localhost/Experiements/webservices/api.php", 
         type:"GET", 
         dataType:"json", 
         data:{type:"login", UserName:"abhishek",Password:"123456"}, 
         ContentType:"application/json", 
         success: function(response){ 
          alert(JSON.stringify(response)); 
         }, 
         error: function(err){ 
          alert(JSON.stringify(err)); 
         } 
        }) 
       }); 
      }); 
     </script> 
    </head> 
    <body> 
     <input type="button" id="btnLogin" name="btnLogin" value="Login"/> 
    </body> 
</html> 

api.php

<?php 
header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim ajax Calling request 
mysql_connect("localhost","root","1234"); 
mysql_select_db("demo"); 
if(isset($_GET['type'])) 
{ 
    if($_GET['type']=="login"){ 
     $username=$_GET['UserName']; 
     $Password=$_GET['Password']; 
     $query="Select * from registration where UserName='$username' and Password='$Password'"; 
     $result=mysql_query($query); 
     $totalRows=mysql_num_rows($result); 
     if($totalRows>0){ 
      $recipes=array(); 
      while($recipe=mysql_fetch_array($result, MYSQL_ASSOC)){ 
       $recipes[]=array('User'=>$recipe); 
      } 
      $output=json_encode(array('Users'=>$recipes)); 
      echo $output; 
     } 
    } 
} 
else{ 
    echo "Invalid format"; 
} 

내 고객 코드 인 test.html를하고, 사용자 이름은 루트과 암호입니다 이고 표는 등록 JQuery와 #에서 선택하여 버튼에 id 추가 .SO 그 id (하지 그 name 속성)에서 요소를 선택하는 데 사용되는 enter image description here

+0

위의 작업 코드는 @Indra Kumar S 덕분에 오류를 수정했습니다. jquery-1.8.2.js를 다운로드하고 Scripts 폴더에 저장하면 작동합니다. – geeks

+0

이 질문에 [XY 문제] (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)가있는 것 같으며 제목을 변경하여 특정 구현을 디버깅하는 방법을 나타냅니다. . 질문에 대해 충분히 멀리 좁혀지지 않았다고 제안하는 답변에 직접적으로 언급하지 않고 어떻게 문구를 쓰는지 잘 모르겠습니다. – Anko

답변

3

.

<input type="button" id="btnLogin" name="btnLogin" value="Login"/> 

이제 $("#btnLogin").click() 기능이 작동합니다.

+0

감사 코드 – geeks

2

오류 1 : 입력 한 내용이 누락되었습니다. 추가 그것을

<input type="button" id="btnLogin" name="btnLogin" value="Login"/> 

오류 2 : 귀하의 아약스 URL을 PHP로 끝나는하지만 당신은 자바 스크립트 #btnLogin 그것을 참조하는 input 요소에

url:"http://localhost/Experiements/webservices/api.php", 
+0

감사합니다, 내 코드에 오류 1이 있었지만 오류 2가 없습니다. html이 아닌 .php를 언급했습니다. 감사합니다. – geeks

+0

하지만 "내 서버 코드는 api.html"입니다. ..... api.html과 api.php는 똑같지 않습니까? –

+0

오 ... 감사합니다. – geeks

0

사용 id 속성 api.html 언급 한

+0

고맙습니다. – geeks

관련 문제