dongzhan8001 2016-02-24 20:53 采纳率: 0%
浏览 64
已采纳

获取网页的内容并放在数组或字符串中

I am not really sure how to phrase this question, so I will just ask the best I can.

I would like to know how to grab the contents of a webpage and place it in a string or array so I can parse the data.

This is the webpage: https://campusdata.uark.edu/api/buses?callback=?&routeIds=8

The webpage returns something like this:

?([{"id":25,"fleet":15,"name":"Fleet 15","description":"","zonarId":9,"gpsId":"8061088","latitude":"36.0680039","longitude":"-94.1758039","speed":0.000,"heading":89.700,"power":true,"date":"\/Date(1456339080000)\/","color":"#0090ff","routeName":"Blue","routeId":8,"distance":9999999999,"nextStop":"Garland Center","nextArrival":"8 mins"},{"id":33,"fleet":6,"name":"Fleet 6 ","description":"","zonarId":13,"gpsId":"8061090","latitude":"36.0818423","longitude":"-94.1707598","speed":0.000,"heading":181.700,"power":true,"date":"\/Date(1456339200000)\/","color":"#0090ff","routeName":"Blue","routeId":8,"distance":2.31887983012931,"nextStop":"South Creekside","nextArrival":"1 mins"}]);

I am not sure the best way to go about this... AJAX through JQuery? Maybe a php call? I don't know.

I have searched for this, but like I said, I don't know exactly how to phrase the question, so my search results have been sporadic at best. Can someone help me please?

  • 写回答

3条回答 默认 最新

  • douan4106 2016-02-24 21:06
    关注

    Seems like a JSONP call. You can use jQuery to easily fetch the data from the API end point. Please see the example below:

    $.ajax({
            url: "https://campusdata.uark.edu/api/buses?callback=?&routeIds=8",
    
            // The name of the callback parameter
            jsonp: "callback",
    
            // Tell jQuery we're expecting JSONP
            dataType: "jsonp",
    
            data: {},
    
            // Work with the response
            success: function( response ) {
                console.log( response ); // server response
            }
        });
    

    Here is a jsfiddle with working example. Please make sure to include jquery in the page before trying this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示