I am using WebSQL. I am trying to add data in Async Block which is making data not to be inserted. Code is given below:
我正在使用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 Dump
{"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"}]}
1 个解决方案
#1
0
Try this ;)
试试这个 ;)
Problem in this loop condition:
此循环条件中的问题:
for(var a = 0; a <= data.length; a++) {
^
Here you are starting from 0
and looping to data.length
在这里,您从0开始并循环到data.length
So to loop with arrays as array index starts from 0
loop till a <= data.length - 1
OR a < data.length
所以要循环使用数组作为数组索引从0循环开始直到<= data.length - 1或
for(var a = 0; a < data.length; a++) {
OR
for(var a=0; a <= (data.length - 1); a++) {
Instead of for
loop you can use for...in
like this:
而不是for循环,你可以使用...像这样:
for(var index in data){
alert(data[index].title);
}
#1
0
Try this ;)
试试这个 ;)
Problem in this loop condition:
此循环条件中的问题:
for(var a = 0; a <= data.length; a++) {
^
Here you are starting from 0
and looping to data.length
在这里,您从0开始并循环到data.length
So to loop with arrays as array index starts from 0
loop till a <= data.length - 1
OR a < data.length
所以要循环使用数组作为数组索引从0循环开始直到<= data.length - 1或
for(var a = 0; a < data.length; a++) {
OR
for(var a=0; a <= (data.length - 1); a++) {
Instead of for
loop you can use for...in
like this:
而不是for循环,你可以使用...像这样:
for(var index in data){
alert(data[index].title);
}