Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to do AJAX cross domain calls ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 253
    Comment on it

    I was facing issue to post data by a form to foreign domain. I am aware of "AJAX cross-domain policy". So we just can't call "http://yahoo.com" over ajax http request and display the results on my page. So after researching I found following:

    i) To enable cross domain POST you have to enable 'Cross-Origin Resource Sharing' on both servers.

    ii) Another easy way is to get cross-domain data using AJAX is to use a server side language as the proxy. Here's a small sample how to implement that using jQuery:

     

    jQuery:

     

    $.ajax({
        url: 'proxy.php',
        type: 'POST',
        data: {
            address: 'http://www.yahoo.com'
        },
        success: function(response) {
            // response now contains full HTML of yahoo.com
        }
    });

    PHP (proxy.php):

     

    echo file_get_contents($_POST['address']);

    Thanks for reading the blog

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: