如何将外部页面的内容作为javascript对象获取?

时间:2022-01-31 21:13:13

So I know there are many ways to get the content of an external page as a string (for example with PHP) but is there a way to get it as a javascript/JSON object (a DOM Window object for example) so I can easily manipulate it? I was thinking perhaps working with iframes, but as always, the same origin policy applies. I feel that this may not be possible by design, but I am interested to try. Does anyone have a creative solution for this? I don't want to use flash or sliverlight (because of iPhone issues).

因此,我知道有很多方法可以将外部页面的内容作为字符串(例如PHP),但是有没有一种方法可以将其作为javascript/JSON对象(例如DOM Window对象),以便我能够轻松地操作它呢?我在想,也许我在用iframe,但和往常一样,同源策略也适用。我觉得这可能是不可能的设计,但我有兴趣尝试。有人对此有创意的解决方案吗?我不想使用flash或sliverlight(因为iPhone的问题)。

3 个解决方案

#1


2  

The only way to do it is through your server. Expose some AJAX method for the purpose of fetching external webpages (or one specific webpage) and let the server do the job instead of the client.

唯一的方法是通过您的服务器。为了获取外部网页(或一个特定的网页)而公开一些AJAX方法,并让服务器代替客户端来完成这项工作。

Added:

补充道:

For PHP, you can do it this way:

对于PHP,您可以这样做:

  1. Create file getremotecontent.php
  2. 创建文件getremotecontent.php
  3. Using file_get_contents fetch the URL you desire (in PHP file):

    使用file_get_contents获取所需的URL(在PHP文件中):

    $pagestr = file_get_contents('http://www.example.com/');
    
  4. Using json_encode encode the resulting string into JSON object.

    使用json_encode将结果字符串编码为JSON对象。

    $pagejson = json_encode($pagestr);
    
  5. Next, send the appropriate headers:

    接下来,发送适当的标题:

    header('Cache-Control: no-cache, must-revalidate');
    header('Content-type: application/json; charset=utf-8');
    
  6. Finally, send the content of the encoded string:

    最后,发送编码字符串的内容:

    print $pagejson;
    
  7. In Javascript, make a Ajax (XHR) request to getremotecontent.php. Server will get you your webpage, encode it into JSON format and give it back for you to process on the client side.

    在Javascript中,向getre莫特.php发出Ajax (XHR)请求。服务器将为您获取您的网页,将其编码为JSON格式,并将其返回给您在客户端进行处理。

#2


1  

No. For security reasons, you can only access pages from the same domain as the one which runs your JavaScript. This is called same origin policy.

不。出于安全原因,您只能访问与运行JavaScript的域相同的页面。这被称为同源策略。

#3


-1  

This is possible with jQuery

这在jQuery中是可能的。

http://api.jquery.com/load/

http://api.jquery.com/load/

#1


2  

The only way to do it is through your server. Expose some AJAX method for the purpose of fetching external webpages (or one specific webpage) and let the server do the job instead of the client.

唯一的方法是通过您的服务器。为了获取外部网页(或一个特定的网页)而公开一些AJAX方法,并让服务器代替客户端来完成这项工作。

Added:

补充道:

For PHP, you can do it this way:

对于PHP,您可以这样做:

  1. Create file getremotecontent.php
  2. 创建文件getremotecontent.php
  3. Using file_get_contents fetch the URL you desire (in PHP file):

    使用file_get_contents获取所需的URL(在PHP文件中):

    $pagestr = file_get_contents('http://www.example.com/');
    
  4. Using json_encode encode the resulting string into JSON object.

    使用json_encode将结果字符串编码为JSON对象。

    $pagejson = json_encode($pagestr);
    
  5. Next, send the appropriate headers:

    接下来,发送适当的标题:

    header('Cache-Control: no-cache, must-revalidate');
    header('Content-type: application/json; charset=utf-8');
    
  6. Finally, send the content of the encoded string:

    最后,发送编码字符串的内容:

    print $pagejson;
    
  7. In Javascript, make a Ajax (XHR) request to getremotecontent.php. Server will get you your webpage, encode it into JSON format and give it back for you to process on the client side.

    在Javascript中,向getre莫特.php发出Ajax (XHR)请求。服务器将为您获取您的网页,将其编码为JSON格式,并将其返回给您在客户端进行处理。

#2


1  

No. For security reasons, you can only access pages from the same domain as the one which runs your JavaScript. This is called same origin policy.

不。出于安全原因,您只能访问与运行JavaScript的域相同的页面。这被称为同源策略。

#3


-1  

This is possible with jQuery

这在jQuery中是可能的。

http://api.jquery.com/load/

http://api.jquery.com/load/