【问题标题】:How add dynamically JSON data to the JQuery Mobile List view in PhoneGap application?如何将 JSON 数据动态添加到 PhoneGap 应用程序中的 JQuery Mobile List 视图?
【发布时间】:2015-01-06 11:18:31
【问题描述】:

我正在以 K.M. 的给定距离获取拼贴的 JSON 数据列表。现在我必须将此拼贴列表添加到 JQuery Mobile 列表视图。在注册页面学生信息将填写。然后在按下注册按钮后,javascript 将执行,在 Java 脚本中,我们将距离作为参数传递,在服务器上将获取位于给定距离内的拼贴列表。我故意没有提到经度和纬度简化原因。 Letter 我们将传递两个参数当前的经度和纬度而不是距离。现在获取的数据将以 json 格式生成,我必须将此信息添加到 ul view 下的页面 CollageOptions请告诉我我应该在里面写什么 成功:javascript 中的函数(响应)。 数据库的结构是

<div data-role="page" id="register" data-theme="d" style="width: 100%;">           
    Name :<input type="text" placeholder="Name" name="userName">
    Email: <input type="text" placeholder="Email" name="eMail">
    Mobile: <input type="number" placeholder="Mobile" name="mobNumber">
    Distance: <input type="text" id="distance_id" name="distance" />
    <a href="#CollageOptions" class="ui-btn" style="background-color: #B6B6BC; color: black;"                         
       id="btnReg">Register</a>
</div>

<div data-role="page" id="CollageOptions">
<div style="width: 100%; margin-top:21%; margin-left: 1%; color: black; id="details" data-  
 role="listview">

   <ul id="listOfCollage" data-role="listview">
        Here I have to list list of collage available with in given distance
   </ul>
</div>

现在假设距离是 35 公里。然后将获取拼贴列表

<?php
header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim ajax Calling request
mysql_connect("localhost","root","1234");
mysql_select_db("foodybuddy");
if(isset($_GET['type']))
{
    if($_GET['type']=="login"){
        $distanceInKM=$_GET['Distance'];       
        $query="Select * from collage where Distance<='$distanceInKM'";
        $result=mysql_query($query);
        $totalRows=mysql_num_rows($result); 
        if($totalRows>0){
            $recipes=array();
            while($recipe=mysql_fetch_array($result, MYSQL_ASSOC)){
                $recipes[]=array('Chef'=>$recipe);
            }
            $output=json_encode(($recipes));
            echo $output;
        }
    }
}
else{
    echo "Invalid format";
}

<script> 
            $(document).ready(function(){  
               $("#btnReg").click(function(){  
                  var distance = document.getElementById('distance_id').value;                
                  $.ajax({ 
                              url:"http://192.168.1.4/Experiements/webservices/getBuddy.php", 
                              type:"GET", 
                              dataType:"json", 
                              data:{type:"login", Distance:distance}, 
                              ContentType:"application/json", 
                              success: function(response){



                              }, 
                              error: function(err){ 
                                  alert(JSON.stringify(err)); 
                              } 
                  }) //ajax
               });  //click
            }); //ready
    </script> 

【问题讨论】:

  • 如果你搜索 SO,你会发现很多例子。
  • @Omar,感谢您的重播我是网络技术的新手,所以请告诉我最好的网站,因为我迫切需要它..

标签: javascript jquery json cordova jquery-mobile


【解决方案1】:

您可以使用 each 循环来读取 Json 数据并准备列表项

var output = ""; // declare an empty variable inside click function

$("#listOfFoodOptions").empty(); // empty the list

.
.
.

success: function(response){ 

$.each(response, function(index,value){ // loop through the data

output += "<li>"+value.Chef.Name+"</li>"; 

}); 

$("#listOfFoodOptions").append(output).listview().listview('refresh'); // refresh the list

}

.
.
.

.

【讨论】:

  • @Tasos,感谢您的重播,但是像 $.ajax({,或者我们不需要 $.ajax({ 在 Post 方法中,我正在等待您的回复...
  • @neelabhsingh 它是您所拥有的简化版本,减去 GET ofcource。看这里api.jquery.com/jquery.post
  • @Tasos ,我看到你的链接是 HTTP 发布请求,但我需要 XMLHTTP 请求,对不起,但我是 webTechnology 的新手,我得到了这个项目,我需要完成它。
  • @Tasos 正如你所说,GET 不起作用在这个问题中(由我发布)我正在使用 GET stackoverflow.com/questions/27731448/…
  • @neelabhsingh 你是对的。我的错误。我会修改答案以反映这一点。无论如何,如果您想使用 GET ,那么您可以使用 (each) 函数循环遍历数据并制作列表。你试过了吗?
猜你喜欢
  • 2014-08-11
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多