jQuery 如何在jquery中调用外部url?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4613310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call external url in jquery?
提问by
I am trying to put comments on Facebook wall using jquery.
我正在尝试使用 jquery 在 Facebook 墙上发表评论。
But my ajax call not alowing external url .
但是我的 ajax 调用不允许外部 url 。
can anyone explain how can we use external url with jquery ?
谁能解释我们如何在 jquery 中使用外部 url?
below is my code :
下面是我的代码:
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);
}
});
its giving xmlhtttprequest error.
它给出了 xmlhtttprequest 错误。
采纳答案by Ben Everard
All of these answers are wrong!
所有这些答案都是错误的!
Like I said in my comment, the reason you're getting that error because the URL fails the "Same origin policy", but you can still us the AJAX function to hit another domain, see Nick Cravers answer on this similar question:
就像我在评论中所说的那样,您收到该错误的原因是 URL 未通过“同源策略”,但您仍然可以使用 AJAX 函数来访问另一个域,请参阅Nick Cravers 对这个类似问题的回答:
You need to trigger JSONP behavior with $.getJSON() by adding &callback=? on the querystring, like this:
$.getJSON("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&titles="+title+"&format=json&callback=?", function(data) { doSomethingWith(data); });
You can test it here.
Without using JSONP you're hitting the same-origin policy which is blocking the XmlHttpRequest from getting any data back.
您需要通过添加 &callback=? 来触发 $.getJSON() 的 JSONP 行为。在查询字符串上,像这样:
$.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 获取任何数据的同源策略。
With this in mind, the follow code should work:
考虑到这一点,以下代码应该可以工作:
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);
}
});
回答by Fernando
JQuery and PHP
jQuery 和 PHP
In PHP file "contenido.php":
在 PHP 文件“contenido.php”中:
<?php
$mURL = $_GET['url'];
echo file_get_contents($mURL);
?>
In html:
在 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>
回答by Andrei Andrushkevich
it is Cross-site scriptingproblem. Common modern browsers doesn't allow to send request to another url.
这是跨站脚本问题。常见的现代浏览器不允许向另一个 url 发送请求。
回答by Atanas Atanasov
I think the only way is by using internel PHP code like MANOJ and Fernando suggest.
我认为唯一的方法是使用像 MANOJ 和 Fernando 建议的内部 PHP 代码。
curl post/get in php file on your server --> call this php file with ajax
在您的服务器上的 php 文件中 curl post/get --> 用 ajax 调用这个 php 文件
The PHP file let say (fb.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);
Than use AJAX GET to
比使用 AJAX GET
fb.php?commentmeta=your comment goes here
from your server.
从您的服务器。
Or do this with simple HTML and JavaScript from externel server:
或者使用来自外部服务器的简单 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>
回答by ebaum
google the javascript same origin policy
谷歌 javascript 同源政策
in a nutshell, the url you are trying to use must have the same root and protocol. so http://yoursite.comcannot access https://yoursite.comor http://anothersite.com
简而言之,您尝试使用的 url 必须具有相同的根和协议。所以 http://yoursite.com无法访问 https://yoursite.com或 http://anothersite.com
is you absolutely MUST bypass this protection (which is at the browser level, as galimy pointed out), consider the ProxyPass module for your favorite web server.
如果您绝对必须绕过此保护(正如 galimy 指出的那样,这是在浏览器级别),请考虑您最喜欢的 Web 服务器的 ProxyPass 模块。
回答by lampdev
Hi url should be calling a function which in return will give response
嗨 url 应该调用一个函数,作为回报,它将给出响应
$.ajax({
url:'function to call url',
...
...
});
try using/calling API facebook method
尝试使用/调用 API facebook 方法
回答by MANOJ
Follow the below simple steps you will able to get the result
按照以下简单步骤您将能够获得结果
Step 1- Create one internal function getDetailFromExternalin your back end. step 2- In that function call the external url by using cUrl like below function
第 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;
}
Step 3- Call that internal function from your front end by using javascript/jquery Ajax.
第 3 步 - 使用 javascript/jquery Ajax 从前端调用该内部函数。