나는 웹 기술에 새로운 문제가 있습니다. 나는 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
속성)에서 요소를 선택하는 데 사용되는
위의 작업 코드는 @Indra Kumar S 덕분에 오류를 수정했습니다. jquery-1.8.2.js를 다운로드하고 Scripts 폴더에 저장하면 작동합니다. – geeks
이 질문에 [XY 문제] (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)가있는 것 같으며 제목을 변경하여 특정 구현을 디버깅하는 방법을 나타냅니다. . 질문에 대해 충분히 멀리 좁혀지지 않았다고 제안하는 답변에 직접적으로 언급하지 않고 어떻게 문구를 쓰는지 잘 모르겠습니다. – Anko