我有这个 AJAX 调用:
$.ajax({
url: '../php/functions/area_id_to_table_name.php',
type: 'POST',
dataType: 'json',
scriptCharset: "utf-8",
data: { areaOfWorkId: userAreaOfWorkArray }
}).always(nextStep);
function nextStep(a, textStatus, b) {
if (textStatus === 'success') {
// success
alert(a);
} else {
// failure
alert('Failed: ' + b);
}
}
和这个 PHP 代码(URL):
<?php
require_once 'connection.php';
$link = conenct('localhost', 'root', '', 'w_db');
mysql_query( "SET NAMES utf8" );
mysql_query( "SET CHARACTER SET utf8" );
mysql_set_charset('utf8', $link);
Class AreaInfo{
public $areaName;
public $areaId;
public $areaTableName;
}
print_r($_POST['areaOfWorkId']);
$areaOfWorkArray = $_POST['areaOfWorkId'];
$areaInfoArray = Array();
foreach ($areaOfWorkArray as $key){
$key = mysql_real_escape_string($key);
if ($key == 0) continue;
$tmpObj = new AreaInfo();
$tmpObj->areaId = $key;
$query = mysql_query("SELECT areaOfWork, tableName FROM area_of_work WHERE id = '$key'");
$query = mysql_fetch_assoc($query);
$tmpObj->areaName = $query['areaOfWork'];
$tmpObj->areaTableName = $query['tableName'];
array_push($areaInfoArray, $tmpObj);
}
print_r($areaInfoArray);
echo json_encode($areaInfoArray);
?>
我收到此错误的警报:
失败:SyntaxError:JSON.parse:意外字符
在 Chrome 开发人员工具上,似乎一切正常。当我打开 PHP 页面时,我看到以下几行:
注意:未定义索引:第 15 行 C:\xampp\htdocs\job-skills\php\functions\area_id_to_table_name.php 中的 areaOfWorkId
注意:未定义索引:第 16 行 C:\xampp\htdocs\job-skills\php\functions\area_id_to_table_name.php 中的 areaOfWorkId
警告:第 19 行 C:\xampp\htdocs\job-skills\php\functions\area_id_to_table_name.php 中为 foreach() 提供的参数无效