使用JavaScript获取动态网页数据

使用JavaScript获取动态网页数据,javascript,Javascript,有人能告诉我如何使用javascript获取动态网页数据或内容吗 类似于php函数get_file_content 页面\值=获取\文件\内容 但它是用javascript编写的。 可能吗?您必须使用 Javascript中的示例 <!DOCTYPE html> <html> <head> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) {// c

有人能告诉我如何使用javascript获取动态网页数据或内容吗

类似于php函数get_file_content 页面\值=获取\文件\内容

但它是用javascript编写的。 可能吗?

您必须使用

Javascript中的示例

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("GET","Your URL",true);
xmlhttp.send();
}
</script>
</head>
<body>

<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>

</body>
</html>

我想我得到了一个完美的解决方案。这可以由YQL完成。这是代码

$document.readyfunction{ var容器=$'show'; $'.ajaxtrigger'。单击函数{ doAjax$this.attr'href'; 返回false; }; 函数doAjaxurl{ alerturl; 风险值数据=http://ronwe.com/ronwe-marketing-suite/; ifurl.match“^http”{ $.getJSONhttp://query.yahooapis.com/v1/public/yql?+ q=从%20html%20中选择%20*%20,其中%20url%3D%22+ encodeURIComponentdata+ %22&format=xml'&callback=?, 函数数据{ ifdata.results[0]{ var data=filterDatadata.results[0]; container.htmldata; }否则{ var errormsg='错误:无法加载页面。

'; container.htmlerromg; } } ; }否则{ $'target'.loadurl; } } 函数filterDatadata{
data=data.replace/jQuery的.load和.ajax函数用于从web服务器加载数据。另请参阅。但是,除非远程服务器启用了CORS,否则这只会将请求发送回源服务器。我强烈建议使用jQuery的ajax,因为这有各种改进和跨浏览器可靠性修复。通过这个过程,我可以获取本地页面或同一域中的页面,但我想获取外部动态页面。xmlhttp.openGET,;如何获取外部动态页面数据?是的。通过这种方式,您可以处理本地页面,即同一域。要访问另一域中的页面,远程服务器必须启用CORS,然后只有ajax才能获得结果。无论远程服务器是什么您正在尝试通过ajax访问..查看CORS是否已启用
$.ajax({
  url: 'getTwitterFollowers.php',
  type: 'GET',
  data: 'twitterUsername=jquery4u',
  success: function(data) {
    //called when successful
    $('#ajaxphp-results').html(data);
  },
  error: function(e) {
    //called when there is an error
    //console.log(e.message);
  }
});
-----------------------------------------------------
PHP - GET NUMBER FACEBOOK FANS & TWITTER FOLLOWERS
-----------------------------------------------------
< ?php
//get data passed to script
$username = htmlspecialchars(strip_tags($_GET["twitterUsername"]));

//get twitter followers
$api_page = 'http://twitter.com/users/show/' . $username;
$xml = file_get_contents ( $api_page );
$profile = new SimpleXMLElement ( $xml );
$count = $profile->followers_count;
$tfans = strval ( $count );

//get facebook likes
$fuser = json_decode(file_get_contents('http://graph.facebook.com/140918675956744/'));

//return result
echo "jQuery4u has " . $fuser->likes . " Facebook fans and " . $tfans . " Twitter followers.";
?>
<?php
    $callback ='mycallback';

    if(isset($_GET['mycallback']))
    {
        $callback = $_GET['mycallback'];
    }   
    $arr =array();
    $arr['name']="Ravishanker";
    $arr['age']=32; 
    $arr['location']="India";   

    echo $callback.'(' . json_encode($arr) . ')';

?>
$.ajax({
        url : "http://hayageektest.appspot.com/cross-domain-cors/jsonp.php",
        dataType:"jsonp",
        jsonp:"mycallback",
        success:function(data)
        {
            alert("Name:"+data.name+"nage:"+data.age+"nlocation:"+data.location);
        }
    });