It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
7年前关闭。
我正在使用Phonegap/Apache Cordova构建一个混合的Android应用程序我的应用程序应该从我的web api获取数据。我正在使用JSON向应用程序提供数据。所以我得到了以下代码:
function init() {
document.addEventListener('deviceready', onDeviceReady, false);
var url = "http://23.21.128.153:3000/regions.json";var jsonresults;
    $.getJSON(url,function(data){
        jsonresults = data;
          $.each(jsonresults, function(i,v){
          $('#main-content').append('<li>'+jsonresults[i].name+'</li>');
        });
    });
}

在html的主体上,我有一个div,叫做main content。在Eclipse浏览器中,一切都很好,但在Android仿真器上却不起作用。不确定是否有其他方法可以使用JSON从Web API中提取数据,并在获取数据后动态创建HMTL元素。
https://gist.github.com/2956660

最佳答案

从web api获取数据非常容易。试试这个代码。它会解决你的问题,
通过jquery mobile很容易。你可以用普通的Ajax和一点JSON来完成你的工作。我完成了php服务器。这里是示例代码,
通过jquery向服务器发送请求。

$.ajax({
          url: <your Server URL>,
          dataType: 'jsonp',
          jsonp: 'jsoncallback',
          timeout: 5000,
          success: function(data, status){
                             /*Process your response here*/

      jsonresults = data;
      $.each(jsonresults, function(i,v){
      $('#main-content').append('<li>'+jsonresults[i].name+'</li>');
    });

 $.mobile.changePage($('#index')); /*page navigate the particular where data shown*/
 $("#index").trigger("pagecreate"); /*This is like a page refresh and load the injected data in jquery*/
                        }
       });

在php服务器文件中,
<?php
$data="I am sending response to you";

/*you have to mention this callback compulsory*/

echo $_GET['jsoncallback'] . '(' . json_encode($data) . ');';
?>

关于javascript - 从Web API获取JSON数据后,在页面加载时动态添加新的HTML元素。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11109575/

10-09 23:55
查看更多