如果没有插入数据,如何提示错误?

时间:2016-06-17 01:09:13

标签: ajax database validation

    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "vos001_view.aspx/SaveRecordVS",
        data: "{'id':'" + id + "','certType':'" + certType + "', 'Certificate':'" + Certificate + "', 'Place':'" + Place + "', 'Date':'" + Date + "', 'Effective':'" + Effective + "', 'Expiry':'" + Expiry + "', 'Attachment':'" + Attachment + "', 'Remarks':'" + Remarks + "'}",
        success: function (test) {


            alert('Vessel Certificate Insert Successful');

            location.reload();
        },
        Error: function (xhr, ajaxOptions, thrownError) {
            Ext.Msg.alert(xhr.responseText, thrownError);
        }
    });

我正在使用ajax,即使id是重复的,它仍然提示插入成功。 如果有错误并且不允许它进入数据库,如何提示错误? 谢谢。

2 个答案:

答案 0 :(得分:1)

我会在成功函数中调试你的ajax响应,所以如果出现错误,你会在浏览器的控制台中看到它:

success: function(result) {
    console.log(result);
},

此外,我会在测试时注释掉页面重新加载//location.reload();,这样您仍然可以在控制台中读取错误。

至于阻止它不允许它“进入数据库”,这将完全在服务器端完成。

答案 1 :(得分:0)

假设您为任何错误抛出异常。你能试试吗?

$.ajax({
    type: "POST",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    url: "vos001_view.aspx/SaveRecordVS",
    data: "{'id':'" + id + "','certType':'" + certType + "', 'Certificate':'" + Certificate + "', 'Place':'" + Place + "', 'Date':'" + Date + "', 'Effective':'" + Effective + "', 'Expiry':'" + Expiry + "', 'Attachment':'" + Attachment + "', 'Remarks':'" + Remarks + "'}",
    success: function (test) {


        alert('Vessel Certificate Insert Successful');

        location.reload();
    },
    error: function (result) {
        var httStatus = result.status;
        if ( httpStatus == 400 ) {
           alert('error!');
        }
    }
});