How to call external url in jquery?

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 jquery ?

below is my code :

var fbUrl="";

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

its giving xmlhtttprequest error.


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:

function(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="";

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

JQuery and PHP

In PHP file "contenido.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);

            type : 'GET',
            url : 'contenido.php',
            dataType : 'html',
            data: {
                url : pUrl
            success : function(data){                                               

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

it is Cross-site scripting problem. Common modern browsers doesn't allow to send request to another url.

google the javascript same origin policy

in a nutshell, the url you are trying to use must have the same root and protocol. so cannot access or

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.

I think the only way is by using internel PHP code like MANOJ and Fernando suggest.

curl post/get in php file on your server --> call this php file with ajax

The PHP file let say (fb.php):

curl_setopt($ch, CURLOPT_URL,$fbUrl);
curl_setopt($ch, CURLOPT_POST, 1);
// POST data here
curl_setopt($ch, CURLOPT_POSTFIELDS,

// 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

fb.php?commentmeta=your comment goes here

from your server.

Or do this with simple HTML and JavaScript from externel server:

Message: <input type="text" id="message">
<input type="submit" onclick='PostMessage()'>
function PostMessage() {
var comment = document.getElementById('message').value;

Hi url should be calling a function which in return will give response

url:'function to call url',


try using/calling API facebook method

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);
        echo $output;

Step 3- Call that internal function from your front end by using javascript/jquery Ajax.

Need Your Help

How to enable Bootstrap tooltip on disabled button?

javascript jquery html twitter-bootstrap twitter-bootstrap-tooltip

I need to display a tooltip on a disabled button and remove it on an enabled button. Currently, it works in reverse.

Can I use an enum as a property in Objective C

objective-c ios xcode properties enums

I saw it is customed to use a boolean property as a flag. something like that: