使用JQUERY从GET请求显示XML数据

时间:2015-08-25 20:04:19

标签: jquery ajax get

我得到了没有错误的工作请求,但现在我无法显示任何数据。它只显示空白。我按照教程,但它似乎不起作用。没有错误。仔细检查了所有变量名称等。

        

        <div class="row">
            <div class="col-lg-12 text-center" id="dvContent">

            </div>
        </div>
        <!-- /.row -->
    </div>

    <!-- /.container -->

    <!-- jQuery Version 1.11.1 -->
    <script src="js/jquery.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>

    <script>
        var url= "http://www.ctabustracker.com/bustime/api/v1/getpredictions?key=JcCyb84hceaH4zhFyrxgEvfKj&stpid=1876";
        url = 'proxy.php?url='+url;
        $.ajax({
            url: url,
            dataType: 'xml',
            success: function(xml){

                alert(prd);

            $(xml).find('prd').each(function(){

              var sTitle = $(this).find('stpnm').text();

              var sPublisher = $(this).find('prdtm').text();

              $("<li></li>").html(sTitle + ", " + sPublisher).appendTo("#dvContent ul");

            });

          }

        });
     </script>

1 个答案:

答案 0 :(得分:0)

这对我有用 - 将ajax片段简化为变量。

     <div class="col-lg-12 text-center" id="dvContent">
         <ul id="ulContent"></ul>
     </div>


<script type="text/javascript">

    var xml = '<bustime-response><prd><tmstmp>20150825 15:21</tmstmp><typ>A</typ><stpnm>Dearborn & Harrison</stpnm><stpid>1876</stpid><vid>1175</vid><dstp>8261</dstp><rt>62</rt><rtdir>Northbound</rtdir><des>Kinzie/State</des><prdtm>20150825 15:30</prdtm><tablockid>62 -653</tablockid><tatripid>22</tatripid><zone/></prd><prd><tmstmp>20150825 15:20</tmstmp><typ>A</typ><stpnm>Dearborn & Harrison</stpnm><stpid>1876</stpid><vid>1706</vid><dstp>6334</dstp><rt>22</rt><rtdir>Northbound</rtdir><des>Howard</des><prdtm>20150825 15:32</prdtm><tablockid>22 -510</tablockid><tatripid>1040922</tatripid><zone/></prd><prd><tmstmp>20150825 15:19</tmstmp><typ>A</typ><stpnm>Dearborn & Harrison</stpnm><stpid>1876</stpid><vid>7965</vid><dstp>12754</dstp><rt>24</rt><rtdir>Northbound</rtdir><des>Wacker/Clark</des><prdtm>20150825 15:33</prdtm><tablockid>24 -702</tablockid><tatripid>1079773</tatripid><zone/></prd><prd><tmstmp>20150825 15:19</tmstmp><typ>A</typ><stpnm>Dearborn & Harrison</stpnm><stpid>1876</stpid><vid>8022</vid><dstp>14759</dstp><rt>62</rt><rtdir>Northbound</rtdir><des>Kinzie/State</des><prdtm>20150825 15:35</prdtm><tablockid>62 -605</tablockid><tatripid>53</tatripid><zone/></prd><prd><tmstmp>20150825 15:20</tmstmp><typ>A</typ><stpnm>Dearborn & Harrison</stpnm><stpid>1876</stpid><vid>1770</vid><dstp>6954</dstp><rt>22</rt><rtdir>Northbound</rtdir><des>Howard</des><prdtm>20150825 15:36</prdtm><tablockid>22 -508</tablockid><tatripid>1040923</tatripid><zone/></prd><prd><tmstmp>20150825 15:20</tmstmp><typ>A</typ><stpnm>Dearborn & Harrison</stpnm><stpid>1876</stpid><vid>1412</vid><dstp>10645</dstp><rt>22</rt><rtdir>Northbound</rtdir><des>Howard</des><prdtm>20150825 15:45</prdtm><tablockid>22 -506</tablockid><tatripid>1040924</tatripid><zone/></prd><prd><tmstmp>20150825 15:20</tmstmp><typ>A</typ><stpnm>Dearborn & Harrison</stpnm><stpid>1876</stpid><vid>1571</vid><dstp>31931</dstp><rt>24</rt><rtdir>Northbound</rtdir><des>Wacker/Clark</des><prdtm>20150825 15:49</prdtm><tablockid>24 -703</tablockid><tatripid>1079774</tatripid><zone/></prd></bustime-response>';
    $(xml).find('prd').each(function () {

        var sTitle = $(this).find('stpnm').text();

        var sPublisher = $(this).find('prdtm').text();

        $("<li></li>").html(sTitle + ", " + sPublisher).appendTo("#ulContent");

    });

</script>