使用jquery的URL外部数据

时间:2010-06-01 13:01:57

标签: jquery

$(document).ready(function()
{



var response = $.ajax({ type: "GET",   
                          url : "http://www.google.com",   
                        async : false,
                      success : function(resp) {
                                    alert(resp);
                                }
               });



});

没有输出......

2 个答案:

答案 0 :(得分:4)

您正试图打破“ajax same origin policy”。

这意味着,您无法使用Ajax请求访问外部域。 “变通办法”是使用JSONPHTML5CORS。所有这些都必须得到服务器端的支持。

解决此问题的最常见方法是将您的网络服务器用作代理。 让您的服务器发出请求并将结果发送回您的网站。

答案 1 :(得分:0)

我不确定你想要达到的目的但可以尝试像

这样的东西
$('#result').load('ajax/test.html', function() {
  alert('Load was performed.');
});

或者查看http://api.jquery.com/load/

<!DOCTYPE html>
<html>
<head>
  <style>
 body{ font-size: 12px; font-family: Arial; }
 </style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>

<b>Footer navigation:</b>
<ol id="new-nav"></ol>

<script>
  $("#new-nav").load("/ #jq-footerNavigation li");
</script>
</body>
</html>

<强>更新

如果您了解PHP,可以试试.. http://www.wait-till-i.com/2010/01/10/loading-external-content-with-ajax-using-jquery-and-yql/

相关问题