我在我的一个页面中有一个 $.ajax 调用,它链接到一个简单的 php 页面。
我收到关于 error:
属性的警报。我没有在 errorThrown 变量或 jqXHR 变量中得到任何返回。我以前从未做过这种事情,我也看不出我的页面有什么问题。
JQuery $.ajax 调用:
function jsonSync(json) {
$.ajax({
type: 'POST',
url: 'http://www.cubiclesandwashrooms.com/areaUpdate.php',
dataType: 'json',
data: json,
context: this,
success: function () {
},
error: function (jqXHR, textStatus, errorThrown) {
alert('Error has occured! \n ERR.INDEX: Sync failed, ' + jqXHR.responseText + ';' + textStatus + ';' + errorThrown.message);
return false;
}
});
这是我的 PHP 页面:
$JSON = file_get_contents('php://input');
$JSON_Data = json_decode($JSON);
//handle on specific item in JSON Object
$insc_area = $JSON_Data->{'insc_area'};
//mysqlite connection.open() equivilent
$insc_db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno($insc_db)) {
die('Could not connect: ' . mysql_error());
echo "Failed to connect to MySql: " . mysqli_connect_error();
//mysqli_close($insc_db);
}
//$insc_area.length equivilent
$insc_area_size = sizeof($insc_area);
//cycle through reult set
for ($i = 0; $i < $insc_area_size; $i++) {
//assign row to DataRow Equivilent
$rec = $insc_area[$i];
//get specific column values
$area = $rec->{'area'};
$id = $rec->{'srecid'};
//sqlcommand equivilent
$query = "SELECT * FROM insc_products WHERE id='$id' LIMIT 1";
$result = mysqli_query($insc_db, $query);
$num = mysqli_num_rows($result);
//dataReader.Read equivilent
while ($row = $result->fetch_array()) {
$query = "UPDATE insc_products SET area='$area' where id = '$id'";
$res = mysqli_query($insc_db, $query);
//checking if update was successful
if ($res) {
// good
error_log('user update done');
echo 'update was successful';
} else {
error_log('user update failed');
echo 'error in update';
}
}
}
echo 'testing php';
最佳答案
dataType: 'json'
意思是:把 json 还给我。你的 PHP 文件没有返回 json 格式的数据
类似问题:jQuery ajax call returns empty error if the content is empty
要构建 json 响应,请使用返回信息在 php 文件中填充一个数组,并在文件末尾使用 echo json_encode($array);
。如果您使用 dataType:'json'
因为代码是复制/粘贴的,并且您不需要响应是 json 格式,只需删除此选项...
关于javascript - $.ajax 调用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24425079/