简体   繁体   English

如何使用ajax / jquery而不是iframe调用外部页面?

[英]How to call outside page using ajax/jquery instead of using iframe?

I need to call another page for example http://www.google.com to my specific div. 我需要调用另一个页面,例如http://www.google.com到我的特定div。 I had done using ajax... 我已经用过ajax ...

$.ajax({
     url : 'http://www.google.com',
     success : function (data)
     {
         $('#my-div').html(data);
     }
});

But it does not work....Is there any better ways instead of using <iframe> ? 但这不起作用。...是否有更好的方法代替使用<iframe>

Your code will not work due to the same-origin policy (if your ajax code is on www.mysite.com , you can only send ajax requests to other files on www.mysite.com ). 由于同源政策,您的代码将无法工作(如果ajax代码位于www.mysite.com上 ,则只能将ajax请求发送到www.mysite.com上的其他文件)。

Here are 2 common solutions: 以下是2种常见解决方案:

1) If you can write server side code, you can ajax your own domain, scrape www.google.com using cURL and return the response. 1)如果您可以编写服务器端代码,则可以使用自己的域名进行ajax,使用cURL 抓取 www.google.com并返回响应。

2) With ajax you can use JSONP but one condition is that the server (www.google.com in your case) is configured for JSONP responses. 2)使用ajax可以使用JSONP,但条件之一是为JSONP响应配置了服务器(在您的情况下为www.google.com)。 Checkout the jquery doc for how to go about JSONP. 检出jQuery文档以了解如何使用JSONP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM