-1

我想在 java 脚本 Ajax 中从网站获取元标记值请参阅以下代码:

我将如何从响应中获取元标记。而且我没有通过使用 getJSON 得到响应。

4

1 回答 1

0

很简单,只需像这样使用 jQuery:

HTML 代码使用(jQuery 加载方法):

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type='text/javascript'>
$(function () { 
        // pass param url
        var data = 'url=' + 'http://neerajsingh.blog.com/category/java-script/';
        // load meta tag viewport in div
        $("#content").load('process.php ', data, function (response, status, xhr) {
            if (status == "success") {                
                // function with response data
                var viewport = $(response).filter('meta[name="viewport"]').attr("content");             
                $("#content").html(viewport);
            }
            if (status == "error") {
                var msg = "Sorry but there was an error: ";
                alert(msg + xhr.status + " " + xhr.statusText);
            }
        });
    });
</script>
<div id="content"></div>

PHP 代码 (process.php)

<?php
$homepage = file_get_contents($_GET['url']);
echo $homepage;
?>

通过阿贾克斯:

$(function () {
    // pass param url
    var data = 'url=' + 'http://neerajsingh.blog.com/category/java-script/';
    // using ajax
    $.ajax({
        url : 'process.php',
        data : data,
        type : 'GET',
        success : function (response) {
            // function with response data
            var viewport = $(response).filter('meta[name="viewport"]').attr("content");
            // show meta tag value
            $("#content").html(viewport);
        }
    });
});
于 2013-07-17T10:08:44.703 回答