【发布时间】:2014-01-09 22:19:51
【问题描述】:
首先,我很抱歉,因为这是我第一次使用 JSON。
我的网站有一个从服务器请求人员数据的客户端脚本。服务器先查询数据库(使用mysql和mysqli),然后将数据(姓名、年龄等)返回给客户端。
具体来说,我想将一个关联数组从 PhP 端传递到客户端。
在做了一些研究之后,我决定使用 AJAX JSON 调用来做这件事。
客户端调用是这样完成的:
var person_id = $('#my_text_box').val();
$.ajax({
url: 'php/upload/my_server_script.php',
method: 'POST',
data: {id: person_id},
dataType: 'json',
cache: false,
success: function(response_data)
{
alert(response_data['name']); //The server should return an associative array
console.log(response_data);
},
error: function(jqXHR, textStatus, errorThrown)
{
console.log(arguments);
console.log(jqXHR.responseText);
console.log('Error: ' + errorThrown + ' ' + textStatus + ' ' + jqXHR);
}
});
服务器端调用一个方法,该方法将查询数据库并提供具有所请求 ID 的人员的详细信息。
$id = $_POST['id'];
函数 getPersonData($id) { $personData = 数组();
(1 - Connect and SELECT name FROM Persons WHERE id = {$id}
2 - Fill the $personData array with result row
3 - Name will be saved in $personData['name'])
return json_encode($personData);
AJAX 调用失败并出现错误 500 - 内部服务器错误。当我在浏览器上检查服务器响应的内容时(在 Chrome,网络选项卡上),它说没有响应(此请求没有可用的响应数据)。
问题是,这段代码在本地完美运行。但是当我将它上传到我的云 Web 服务器时,我网站中唯一失败的 AJAX 调用是使用 JSON 作为数据传输格式的调用。其他的工作正常。
我尝试过的几件事:
- 首先,检查 PhP 端的数组 是否为空或构建错误。不是,所有正确的值都在那里;
- 其次,包括application/json到云网络服务器mime.type文件(是Apache);
- 然后,在我的服务器端脚本中包含一个 header('Content-Type: application/json');。
- 另外,将“contentType: 'application/json'”添加到客户端$.ajax。
这四个都不起作用。我会忘记什么?
注意:浏览器的日志内容如下:
Arguments[3]
0: Object
1: "error"
2: "Internal Server Error"
callee: function (jqXHR, textStatus, errorThrown)
length: 3
__proto__: Object
*(url of my script file)*
Error: Internal Server Error error [object Object] ^
注意 #2:完整的 PhP 代码:
//Fetch persondata for a specific ID, and encode the data in an array in JSON format
function JSONSelectPersonDataFromID($ID)
{
$personData = array(); //Array or array with results
//If querysuccess, commit. Else, rollback
$querySuccess = True;
//This method opens connection with root user
$conn = OpenDBConn();
$conn->autocommit(False);
try
{
if($videoID > 0)
{
$sql = "SELECT name FROM Persons WHERE id={$id}";
//Debugging
//echo $sql . "<br>";
$persons = mysqli_query($conn, $sql);
if(mysqli_connect_errno($conn))
{
$querySuccess = False;
}
if(isset($persons ) && (count($persons ) > 0))
{
//Loop through every scene
$personData = $persons ->fetch_array(MYSQLI_ASSOC);
}
else
{
return null;
}
}
else
{
$querySuccess = False;
}
}
catch(Exception $e)
{
$querySuccess = False;
}
if(!$querySuccess)
{
//Rollback
$conn->rollback();
die("Transaction failed");
}
else
{
//Commit
$conn->commit();
}
//Close the connection
DBClose($conn);
return json_encode($personData );
}
【问题讨论】:
-
A
500 error表示服务器端(您的 PHP 脚本)发生错误。因为它只在发布 JSON 时发生,所以我会冒险猜测您错误地操纵了 JSON。如果没有看到您的 PHP 代码,这是无法诊断的。 -
您在本地和服务器上使用什么版本的 PHP? json_encode 的行为可能会有所不同。
-
感谢您的反馈 - phpInfo() 通知我,我在我的 Web 服务器上使用“PHP 版本 5.5.3-1ubuntu2.1”。
-
我已将服务器端函数的完整 PhP 代码添加到我的原始帖子中。
-
您是否尝试过像这样在浏览器中加载 php 文件:“my_server_script.php?id=x” 其中 x 是您使用 AJAX 传递的 id 并更改 PHP 代码以使用 GET 方法而不是 POST 看看会发生什么?
标签: php jquery ajax json response