Chrome扩展:没有插入或提取数据

Chrome Extension: Data is not being inserted nor fetched

本文关键字:提取 数据 插入 扩展 Chrome      更新时间:2023-10-11

我正在使用WebSQL。我正在尝试在异步块中添加数据,这使得数据无法插入。代码如下:

function fetchData(){
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "http://localhost/x/fetch.php", true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            // JSON.parse does not evaluate the attacker's scripts.
            var resp = xhr.responseText;
            if(resp != null) {
                var json = JSON.parse(resp)
                console.log(resp);
                var data = json['data'];
                if(data != null) {
                    openDatabase('documents', '1.0', 'documents', 5*1024*1024, function (db) {
                        alert('Called'); // This is called after below two calls.
                        insertRecord(db);
                        fetchRecord(db);
                    });
                    //var dbConnection = openDbConnect();
                    //createTable(dbConnection);
                    for(var a=0;a <= data.length;a++) {
                        alert(data[a].title);
                    }
                }
            }
        }
    }
    xhr.send();
}

JSON转储

{"data":[{"id":"1","title":"- Parts I & II”,”CODE”:”xxx”,”product_url":"http:'/'/www.example.com","image_url":"http:'/'/ecx.images-example.com'/images'/I'/61ujIIMyW7L.jpg","price":"$25.00"},{"id":"2","title”:”AJDJDDJDr”,”Code”:”XX”,”product_url":"http:'/'/www.example.com","image_url":"http:'/'/dc.images-example.com'/images'/I'/41jFVZL72YL.jpg","price":"$10.99"}]}

试试这个;)

此循环条件下的问题:

for(var a = 0; a <= data.length; a++) {
                  ^

这里您从0开始,循环到data.length

因此,从0循环到a <= data.length - 1a < data.length ,循环数组作为数组索引

for(var a = 0; a < data.length; a++) {

for(var a=0; a <= (data.length - 1); a++) {

代替for循环,您可以像这样使用for...in

for(var index in data){
  alert(data[index].title);
}