I am trying to put comments on Facebook wall using jquery.
But my ajax call not alowing external url .
can anyone explain how can we use external url with jq
google the javascript same origin policy
in a nutshell, the url you are trying to use must have the same root and protocol. so http://yoursite.com cannot access https://yoursite.com or 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.
Follow the below simple steps you will able to get the result
Step 1- Create one internal function getDetailFromExternal in your back end. step 2- In that function call the external url by using cUrl like below function
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.
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:
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.
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);
}
});
it is Cross-site scripting problem. Common modern browsers doesn't allow to send request to another url.
JQuery and PHP
In PHP file "contenido.php":
<?php
$mURL = $_GET['url'];
echo file_get_contents($mURL);
?>
In 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>
Hi url should be calling a function which in return will give response
$.ajax({
url:'function to call url',
...
...
});
try using/calling API facebook method