在AJAX中读取和使用JSO数据

时间:2017-10-24 07:51:57

标签: json ajax

我在阅读JS中的JSON数据时遇到了麻烦。

这是代码

//to Sync local db from server db

$('#btnSyncLocalJSON').click(function () {
    //url: 'api/service?revision=' + localStorage.customerRevision,
    //url: 'http://localhost/API/api.php',
    var url = "http://ip.jsontest.com/";  


    $.getJSON(url, function(result){
        console.log(result);
        $.each(result, function(i, field){
            var name = field.ip;
            console.log(name);
            var email = field.ip;
            console.log(email);
            //put data into local IndexedDB
            var transaction = db.transaction(["customers"],"readwrite");
            //Ask for ObjectStore
            var store = transaction.objectStore("customers");

            //Define Customer
            var customer = {
                name: name,
                email: email
            }

            //Perform the Add
            var request = store.add(customer);

            //Success
            request.onsuccess = function(e){
                //window.location.href="index.html";
                console.log('customer added');
            }

            //Error
            request.onerror = function(e){
                alert("Sorry, the customer was not added");
                console.log('Error', e.target.error.name);
            }
            //window.location.href="index.html";
        });
    });

});

var url =“http://ip.jsontest.com/”;给了我这个JSON {ip:“41.190.33.162”}

我试图说明将服务器上的数据加载到indexedDB .`

的概念

1 个答案:

答案 0 :(得分:0)

得到了解决方案。

假设使用result.ip而不是field.ip