AJAX Requesting과 PHP에 대한 멍청한 질문입니다. 내 wamp 서버에서 PHP 파일에 대한 GET 요청을하려고하는데 ResponseText가 비어 있습니다. readyState가 4 일 때 상태 코드를 확인하면 0이됩니다.PHP를 사용하여 AJAX 요청의 responseText가 비어 있습니다.
브라우저에서 PHP 파일을 실행할 때 JSON 객체가있는 배열을 기대할 수 있습니다.
답변을 알고 있습니까?
자바 스크립트 코드 :
this.getCars = function(id) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var that = this;
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4)
{
alert(xmlhttp.status);
//that.lastTableCars = JSON.parse(xmlhttp.responseText);
}
}
xmlhttp.open("GET","http://localhost/getCars.php?q="+id,true);
xmlhttp.send(null);
}
PHP :
하나<?php
$q=$_GET["q"];
$con = mysql_connect('127.0.0.1', 'root', 'root');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("autobay", $con);
$sql= "SELECT * FROM autoos WHERE id = '".$q."'";
$result = mysql_query($sql);
$info = [];
while($row = mysql_fetch_assoc($result)){
$info[] = $row;
}
echo json_encode($info);
mysql_free_result($result);
mysql_close();
당신의 PHP 코드에는'?>'이 없습니다 ... – thejh
당신의 코드는 취약합니다. 더 이상 사용되지 않습니다 (mysql) – Sam
'?>'은 필요하지 않습니다. –