如何从DB(JSON)获取JS的特定条目

时间:2013-05-30 15:44:13

标签: javascript json cordova getjson

我正在尝试做类似字典的事情。这个想法是有一个DB在线。有人可以搜索术语并从数据库中获取描述。

工作原理:

1)将下载db中的完整术语并与searchterm进行比较。每个术语都有其特定的ID。如果searchterm是数据库,则将保存ID。

2)ID将通过DB发送到服务器,并且应该接收描述

获取所有条款的网址

http://s288617660.mialojamiento.es/api.php?rquest=terms

获取说明的网址

http://s288617660.mialojamiento.es/api.php?rquest=answers&param1=ID

现在这是我的搜索功能

<script>

    getAllTerms();


    function getAllTerms(){
        var url = "http://s288617660.mialojamiento.es/api.php?rquest=terms";

       $.get(url, function(data) {

          localStorage.setItem("terms", JSON.stringify(data));
          //alert(JSON.stringify(data));
        }).done(function() { /*alert(localStorage.getItem("terms"));*/ })
          .fail(function() { alert("error"); })
          .always(function() { /*alert("finished");*/ });     
    }


    function findTerm(){
        var content = $("#SearchTerm").val();
        //alert(content);
        var id = 0;

        var data = JSON.parse(localStorage.getItem("terms"));

        $.each(data, function(index, term) {
             //alert("My content " + content + ", term " + term.name);
             if(content == term.name){
                 id = term.id_term;
             }
        });

        //--------------my try to request the description

        var url_answers = "http://s288617660.mialojamiento.es/api.php?rquest=answers&param1="+id;
        //alert(url_answers);
        var answer = "";

        $.get(url_answers, function(data2) {
            localStorage.setItem("answer", JSON.stringify(data2));


            $.each(data2, function(index, object) {

                    answer = object.description+object.id_answer;

            });


        //-----------------end of my try


        //alert(id);
        var htmlStr = "";

        if(parseInt(id) > 0){
            //i found the term
            localStorage.setItem("idterm",id);

            htmlStr = "<li>" + content + "- id: " + id + answer +  "</li>"
        }else{
            //not found 
            htmlStr = "<li>Term not found</li>"
        }

        $('#SearchList').empty();
        $('#SearchList').append(htmlStr);
        $('#SearchList').listview("refresh");

    }


</script>

ID请求工作正常但不是描述请求。为什么? 顺便说一下:这是一个android phonegap项目

0 个答案:

没有答案
相关问题