简体   繁体   English

即使更新数据库,AJAX也不会成功

[英]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.

相关问题 Ajax返回成功但不更新数据库 - Ajax returning success but not updating database 即使成功响应,firebase数据库也不会更新 - firebase database not updating, even though successful response 我的 Ajax 返回成功但未更新 mysql 数据库 - My Ajax is returning success but not updating mysql database Bootstrap模式不会出现,即使它是直接副本 - Bootstrap modal not coming up, even though it is a direct copy 我的RESTful API没有更新数据库,但是ajax返回成功 - My RESTful API isn't updating the database, but ajax returns success JQuery Ajax 正在更新 MySQL 数据库,但未运行成功 Function - JQuery Ajax Updating MySQL Database, But Not Running Success Function 即使成功向API发出请求,也不会触发Ajax成功功能,为什么? - Ajax success function not triggered even though the request to the API is made succesfully why? Internet Explorer仅在jQuery ajax成功响应内执行一次功能,即使有三个请求 - Internet explorer only executing function inside jQuery ajax success response once even though there are three requests 弹出ajax成功 - Pop up on ajax success 即使列表是ngRepeat也不会更新 - ngRepeat not updating even though list is
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM