使用jQuery从外部网站获取价值

时间:2014-10-16 12:41:04

标签: javascript jquery cross-domain

所以我有一个外部页面:

http://api.aghost.net/api/futures/index.cfm?Username=E020521102&Password=ksc0rNX&service=table&style=3&layout=chart&showGrid=0&symbols=@C@3&colorLabels=0&contractFormat=2&dateFormat=short&fontsize=large&timeFormat=m&dateFormat=m

我试图检索" 345' 4",我的代码只是给了我一个对象,当我运行' .toSource'在对象上我收到以下输出:

({0:{}, length:1, prevObject:{0:{location:({}), icpsignup:{0:{}, 1:{}, 2:{}, 3:{}, 4:{}, 5:{}, 6:{}, 7:{}, 8:{}, 9:{}}, jQuery1710019525210189072606:2}, context:{location:({}), icpsignup:{0:{}, 1:{}, 2:{}, 3:{}, 4:{}, 5:{}, 6:{}, 7:{}, 8:{}, 9:{}}, jQuery1710019525210189072606:2}, length:1}, context:{location:({}), icpsignup:{0:{}, 1:{}, 2:{}, 3:{}, 4:{}, 5:{}, 6:{}, 7:{}, 8:{}, 9:{}}, jQuery1710019525210189072606:2}, selector:"div.month.left span.big"})

我做错了什么?我觉得我接近一个解决方案,但有一些我想念的东西..

以下是我的代码:

var data = 'http://api.aghost.net/api/futures/index.cfm?Username=E020521102&Password=ksc0rNX&service=table&style=3&layout=chart&showGrid=0&symbols=@C@3&colorLabels=0&contractFormat=2&dateFormat=short&fontsize=large&timeFormat=m&dateFormat=m';
var $row1 = jQuery("div.month.left span.big"),  $row2 = jQuery("#cpuloaddynamic"), $row3 = jQuery("#meminfodynamic");

$.get("index.html", function(data){
    var $data=$(data);
    $row1.html( $data.find('tbody > tr:first-of-type > td:nth-of-type(5)').html() );
});
alert($row1.toSource());

我从以下帖子开始:jquery: load values from external html and populate many local <div> fields, with one read of the html

这是我编辑过的代码:

var data = 'http://api.aghost.net/api/futures/index.cfm?Username=E020521102&Password=ksc0rNX&service=table&style=3&layout=chart&showGrid=0&symbols=@C@3&colorLabels=0&contractFormat=2&dateFormat=short&fontsize=large&timeFormat=m&dateFormat=m';
var $row1 = jQuery("div.month.left span.big"),  $row2 = jQuery("#cpuloaddynamic"), $row3 = jQuery("#meminfodynamic");

$.get(data, function(data){
    var $data=$(data);
    $row1.html( $data.find('tbody > tr:first-of-type > td:nth-of-type(5)').html() );
    alert( $data.find('tbody > tr:first-of-type > td:nth-of-type(5)').html() );
});

1 个答案:

答案 0 :(得分:1)

好像您要将请求发送到错误的页面。 $ .get的第一个参数(您的“index.html”)是必须接收您的请求的页面的网址(我认为这是您在“数据”变量中的网址)。函数中的“data”参数应该包含您期望的对象。

然而,即使您修复此问题,我也不认为代码有效,因为使用jquery,您可以将请求发送到您域中的网址。