即使更新数据库,AJAX也没有取得成功

时间:2015-08-18 18:13:36

标签: javascript php ajax

我的php正在更新表但是在javascript中没有刷新尝试了几种不同的方法来做这个并没有什么工作。 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

var data = {
    "formId": formID,
    "newSuper": newSuper
};
data = $.param(data);
$.ajax({
    type: "POST",
    dataType: "json",
    url: "src/GetInfo.php", 
    data: data,
success: function() {
    location.reload();
}

});

2 个答案:

答案 0 :(得分:0)

我首先修改代码如下:

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
    }
});

另外,这可能是在黑暗中拍摄但参数dataType在过去的某个时候给了我一些问题,所以如果你确定你的php脚本返回的json,你可以使用eval函数来jsonify回应

$.ajax({
    ...

    // Remove data type
    // dataType: "json", 

    ...

    success: function(plainTextResponse) {
        // Eval response, NOT SAFE! But working
        var jsonResponse = eval('('+ plainTextResponse +')');

        ...
    }
});

答案 1 :(得分:0)

您的ajax期待json数据,而您的php发送格式错误的json字符串。发送正确的json字符串,您的脚本将正常运行。

你的php json_encode应该是这样的:

$data = json_encode($return);
echo $data;