【问题标题】:How to call external url in jquery?如何在 jquery 中调用外部 url?
【发布时间】:2011-06-04 12:58:09
【问题描述】:

我正在尝试使用 jquery 将 cmets 放在 Facebook 墙上。

但我的 ajax 调用不支持外部 url。

谁能解释我们如何在 jquery 中使用外部 url?

下面是我的代码:

var fbUrl="https://graph.facebook.com/16453004404_481759124404/comments?access_token=my_token";

$.ajax({        
    url: fbURL ,
    data: "message="+commentdata,
    type: 'POST',
    success: function (resp) {
        alert(resp);
    },
    error: function(e){
        alert('Error: '+e);
    }  
});

它给出 xmlhtttprequest 错误。

【问题讨论】:

标签: jquery ajax cross-domain-policy


【解决方案1】:

所有这些答案都是错误的!

就像我在评论中所说的那样,您收到该错误的原因是因为 URL 失败了“Same origin policy”,但是您仍然可以使用 AJAX 功能来访问另一个域,请参阅Nick Cravers answer on this similar question

您需要触发 JSONP 行为 与 $.getJSON() 通过添加 &callback=? 在查询字符串上,像这样:

$.getJSON("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&titles="+title+"&format=json&callback=?",
function(data) {
    doSomethingWith(data); 
}); 

你可以在这里测试一下。

如果不使用 JSONP,您会遇到 阻塞的同源策略 XmlHttpRequest 从获取任何 数据返回。

考虑到这一点,下面的代码应该可以工作:

var fbURL="https://graph.facebook.com/16453004404_481759124404/comments?access_token=my_token";

$.ajax({
    url: fbURL+"&callback=?",
    data: "message="+commentdata,
    type: 'POST',
    success: function (resp) {
        alert(resp);
    },
    error: function(e) {
        alert('Error: '+e);
    }  
});

【讨论】:

  • $.getJSON 用于获取日期。这里我想在墙上发布数据
  • 错误回调有三个参数(不确定是否正确),第一个是XHR,第二个是textStatus,第三个是errorThrown,你能提醒这些并告诉我们他们说什么吗?
  • 因为它回答了原作者的问题?
【解决方案2】:

JQuery 和 PHP

在 PHP 文件“contenido.php”中:

<?php
$mURL = $_GET['url'];

echo file_get_contents($mURL);
?>

在html中:

<script type="text/javascript" src="js/jquery/jquery.min.js"></script>
<script type="text/javascript">
    function getContent(pUrl, pDivDestino){
        var mDivDestino = $('#'+pDivDestino);

        $.ajax({
            type : 'GET',
            url : 'contenido.php',
            dataType : 'html',
            data: {
                url : pUrl
            },
            success : function(data){                                               
                mDivDestino.html(data);
            }   
        });
    }
</script>

<a href="#" onclick="javascript:getContent('http://www.google.com/', 'contenido')">Get Google</a>
<div id="contenido"></div>

【讨论】:

【解决方案3】:

这是Cross-site scripting 问题。常见的现代浏览器不允许向另一个 url 发送请求。

【讨论】:

  • 你的意思是“同源政策”。
  • @Andrei Andrushkevich,正确!那么你有什么出路或解决方法吗?我的意思是,如果要专门调用另一个 URL,应该怎么做?
【解决方案4】:

我认为唯一的方法是使用像 MANOJ 和 Fernando 建议的内部 PHP 代码。

curl post/get in your server 上的 php 文件 --> 用 ajax 调用这个 php 文件

假设 PHP 文件 (fb.php):

$commentdata=$_GET['commentdata'];
$fbUrl="https://graph.facebook.com/16453004404_481759124404/comments?access_token=my_token";
curl_setopt($ch, CURLOPT_URL,$fbUrl);
curl_setopt($ch, CURLOPT_POST, 1);
// POST data here
curl_setopt($ch, CURLOPT_POSTFIELDS,
        "message=".$commentdata);

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);
echo $server_output;
curl_close ($ch);

比使用 AJAX GET 来

fb.php?commentmeta=your comment goes here

来自您的服务器。

或者使用来自外部服务器的简单 HTML 和 JavaScript 执行此操作:

Message: <input type="text" id="message">
<input type="submit" onclick='PostMessage()'>
<script>
function PostMessage() {
var comment = document.getElementById('message').value;
    window.location.assign('http://yourdomain.tld/fb.php?commentmeta='+comment)
}
</script>

【讨论】:

    【解决方案5】:

    google javascript 同源策略

    简而言之,您尝试使用的 url 必须具有相同的根目录和协议。 所以 http://yoursite.com 无法访问 https://yoursite.comhttp://anothersite.com

    您是否绝对必须绕过此保护(正如 galimy 指出的那样,这是在浏览器级别),请考虑为您最喜欢的网络服务器使用 ProxyPass 模块。

    【讨论】:

      【解决方案6】:

      你好 url 应该调用一个函数,作为回报会给出响应

      $.ajax({
      url:'function to call url',
      ...
      ...
      
      });
      

      尝试使用/调用 API facebook 方法

      【讨论】:

      • 这里不是url问题,绕过同源策略。
      【解决方案7】:

      按照以下简单步骤,您将能够得到结果

      第 1 步 - 在您的后端创建一个内部函数 getDetailFromExternal。 第 2 步- 在该函数中,使用 cUrl 调用外部 url,如下函数

       function getDetailFromExternal($p1,$p2) {
      
              $url = "http://request url with parameters";
              $ch = curl_init();
              curl_setopt_array($ch, array(
                  CURLOPT_URL => $url,
                  CURLOPT_RETURNTRANSFER => true            
              ));
      
              curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
              $output = curl_exec($ch);
              curl_close($ch);
              echo $output;
              exit;
          }
      

      第 3 步 - 使用 javascript/jquery Ajax 从前端调用该内部函数。

      【讨论】:

        猜你喜欢
        • 2018-09-26
        • 2013-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多