Javascript / Ajax成功请求从websql删除记录

时间:2012-01-30 09:55:36

标签: javascript ajax function scope

我使用以下代码查询我的websql数据库以查看是否有任何记录。

如果找到记录,那么我使用ajax请求将这些提交到asp页面,然后将页面信息推送到sql server。

到目前为止一切都很好但是我现在只想在ajax请求返回成功时从WebSql数据库中删除每条记录。

我不能为了我的生活,当他们嵌套这个大的时候正确地了解javascript函数和范围(我现在只写了几个星期的js / jquery)。

我知道我需要捕获已设置为变量vID的ID,然后执行另一个sql语句来删除记录,嵌套在我的ajax调用的成功标准中。我无法正确引用vID然后我不知道如何设置调用再次执行sql。

function Web_SQL_Storage_Open_And_Check() {
    if (window.openDatabase) {
        var db = openDatabase("ER_Nonconformance", "1.0", "Non-conformance Report DB", 4 * 1024 * 1024); //creates our database if it does not exist at 4mb size
        var db2 = db.transaction(function(tx) {
          tx.executeSql("CREATE TABLE IF NOT EXISTS tbllog (ID INTEGER PRIMARY KEY ASC AUTOINCREMENT, DteOccur datetime, Pdetected nvarchar(50), DeptRaisedBy int, DeptResp int, NCDescrip nvarchar(255), NCCause nvarchar(255), NCImmediateAct nvarchar(255), NCLocation nvarchar(100), PNumOrRef nvarchar(30), EventCat int, ReportedEmailAddy nvarchar(100), Location_Category int)");
          tx.executeSql("SELECT * FROM tbllog", [], function(tx, result) {


                                if (result.rows.length > 0) {

                                alert("we need to submit")
                                for (var i = 0; i < result.rows.length; i++) {
                                //Submit values to the asp page using the below loop
                                var vID = (result.rows.item(i)['ID'])
                                var vPdetected = result.rows.item(i)['Pdetected']
                                var vDteOccur = result.rows.item(i)['DteOccur']
                                var vDeptRaisedBy = result.rows.item(i)['DeptRaisedBy']
                                var vDeptResp = result.rows.item(i)['DeptResp']
                                var vNCDescrip = result.rows.item(i)['NCDescrip']
                                var vNCCause = result.rows.item(i)['NCCause']
                                var vNCImmediateAct = result.rows.item(i)['NCImmediateAct']
                                var vNCLocation = result.rows.item(i)['NCLocation']
                                var vPNumOrRef = result.rows.item(i)['PNumOrRef']
                                var vEventCat = result.rows.item(i)['EventCat']
                                var vReportedEmailAddy = result.rows.item(i)['ReportedEmailAddy']
                                var vLocation_Category = result.rows.item(i)['Location_Category']

                                var request = $.ajax({
                                url: "untitled.asp",
                                type: "post",
                                data: {
                                DteOccur: vDteOccur,
                                Pdetected: vPdetected,
                                DeptRaisedBy: vDeptRaisedBy,
                                DeptResp: vDeptResp,
                                NCDescrip: vNCDescrip,
                                NCCause: vNCCause,
                                NCImmediateAct: vNCImmediateAct,
                                NCLocation: vNCLocation,
                                PNumOrRef: vPNumOrRef,
                                EventCat: vEventCat,
                                ReportedEmailAddy: vReportedEmailAddy,
                                Location_Category: vLocation_Category
                                },
                                dataType: "html"

                                }

                                );

                                request.done(function() { alert("done"); })
                                request.fail(function() { alert("error"); })
                                request.always(function() { alert("complete"); });


                                }



                                } 
                                else {
                                //no records found so do nothing
                                alert("do nothing")
                                } //end of if statement

            }, function(tx, error) {
                alert("error retrieving")
            })




          //tx.executeSql();


        });
    }

}

1 个答案:

答案 0 :(得分:0)

定义var vID外部函数,而不是内部,并为您提供正确的参考。