[英]AJAX not coming up a success even though its updating the database
My php is updating the table but not refreshing in javascript have tried several different ways of doing this and nothing is working. 我的php正在更新表格,但未在javascript中刷新,因此尝试了几种不同的方法来执行此操作,但没有任何效果。 PHP 的PHP
$sql = "UPDATE INTOXDM.ASTP_FORM SET SUPERVISOR_EID = '".$newSuper."' WHERE FORMID = '".$formId."'";
$row = $xdm->fetch($sql);
$return["color"] = $row['APPRENTICE_SIGNATURE'];
$return["json"] = json_encode($return);
echo json_encode($return);
?>
Javascipt Javascipt
var data = {
"formId": formID,
"newSuper": newSuper
};
data = $.param(data);
$.ajax({
type: "POST",
dataType: "json",
url: "src/GetInfo.php",
data: data,
success: function() {
location.reload();
}
});
I'd start by modifing the code like this: 我将从修改如下代码开始:
var data = {
"formId": formID,
"newSuper": newSuper
};
// No need for serialization here,
// the 'data' parameter of jQuery.ajax accepts JS object
// data = $.param(data);
$.ajax({
type: "POST",
dataType: "json",
url: "src/GetInfo.php",
data: data,
// As suggested by Rocket Hazmat, try to add an error callback here
error: function(jQueryXHR, textStatus, errorMessage) {
console.log("Something went wrong " + errorMessage);
},
success: function(jsonResponse) {
// Try to reference the location object from document/window
// wd = document or window as seen here http://stackoverflow.com/questions/2624111/preferred-method-to-reload-page-with-javascript
// Also watch out, usually browsers require a user confirmation before reloading if the page contains POST data
// One of these should be fine
wd.location.assign(wd.location.href) : go to the URL
wd.location.replace(wd.location.href) : go to the URL and replace previous page in history
wd.location.reload(<true/false/blank>) : reload page from server/cache/cache
}
});
Also, this might be a shot in the dark but the parameter dataType
gave me problems sometime in the past, so if you are sure about the json returned by your php script, you could use the eval function to jsonify the response 另外,这可能是在黑暗中拍摄的,但是参数dataType
在过去的某个时候给我带来了问题,因此,如果您确定php脚本返回的json,则可以使用eval函数对响应进行json处理
$.ajax({
...
// Remove data type
// dataType: "json",
...
success: function(plainTextResponse) {
// Eval response, NOT SAFE! But working
var jsonResponse = eval('('+ plainTextResponse +')');
...
}
});
Your ajax is expecting json
data and your php is sending malformed json
string. 您的ajax需要json
数据,而您的php发送格式错误的json
字符串。 Send a correct json
string and your script will work fine. 发送正确的json
字符串,您的脚本将正常运行。
Your php json_encode should be like this: 您的php json_encode应该是这样的:
$data = json_encode($return);
echo $data;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.