如何调用外部URL jQuery的?
-
26-09-2019 - |
题
我试图把使用jQuery的Facebook墙上留言。
但我的Ajax调用不alowing外部URL。
谁能解释一下我们如何使用外部URL与jQuery?
下面是我的代码:
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错误。
解决方案
这些答案全部是错误的!
就像我在我的评论,因为URL失败“同你得到错误的原因说来源政策“但你仍然可以我们AJAX功能打到另一个域,看到的本上类似的问题尼克Cravers答案:
您需要触发JSONP行为 用$ .getJSON()加入&回调=? 在查询字符串,如下所示:
$.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);
}
});
其他提示
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>
它是跨站点脚本问题。常见的现代浏览器不允许请求发送到另一个URL。
谷歌的JavaScript同源策略
概括地说,你正在试图使用的URL必须具有相同的根和协议。 所以 http://yoursite.com 不能上网 https://yoursite.com 或 http://anothersite.com
是你绝对必须绕过这种保护(这是在浏览器的水平,galimy指出的),考虑的ProxyPass模块为您最喜爱的Web服务器。
我认为唯一的方式是通过使用个内PHP代码等MANOJ和Fernando建议。
卷曲后/获得php文件服务器上 - >调用使用Ajax
在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
从你的服务器。
或者与来自externel服务器简单的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>
您好的URL应该被调用函数作为回报会给响应
$.ajax({
url:'function to call url',
...
...
});
尝试使用/主叫API的Facebook方法
按照下面的简单步骤,你将能够得到的结果。
步骤1-创建一个内部功能的 getDetailFromExternal 强>在后端。 步骤2-在该功能通过使用卷成那样下面函数调用的外部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的。