I have this AJAX call:
$.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);
}
}
and this PHP code (the 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);
?>
I am getting alert of this error:
Failed: SyntaxError: JSON.parse: unexpected character
On the Chrome developer tool it seems that all works fine. When I open the PHP page i see this lines:
Notice: Undefined index: areaOfWorkId in C:\\xampp\\htdocs\\job-skills\\php\\functions\\area_id_to_table_name.php on line 15
Notice: Undefined index: areaOfWorkId in C:\\xampp\\htdocs\\job-skills\\php\\functions\\area_id_to_table_name.php on line 16
Warning: Invalid argument supplied for foreach() in C:\\xampp\\htdocs\\job-skills\\php\\functions\\area_id_to_table_name.php on line 19
When you call print_r
, you put garbage (I mean not JSON) in the response. The browser can't interpret it as JSON.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.