0

I am sending ajax call on different server using script mentioned below.

$(document).ready(function() {
var uniqcod=$(".piczhu-widget").attr('id'); 

    $.ajax({
        url:'File Path...',
        type:'post',
        data:{uniId:uniqcod},
        success: function(result){
            $('.abcClass').html(result);
            }
        });
    });

Script is not receiving any response. This script is working fine on the same server. Is there any additional parameter to use to send call on different server?

Charles
  • 50,943
  • 13
  • 104
  • 142
Sid Connar
  • 27
  • 2
  • 5
  • File Path... ... ? tried different one ? – internals-in Apr 08 '13 at 05:40
  • Is this answer any help, its about the jQuery cross-site call. Worst case is you need to edit server side code as well to handle all use cases. http://stackoverflow.com/questions/6101731/jquery-external-ajax-call-not-working-in-ie/10618712#10618712 – Whome Apr 08 '13 at 05:51

4 Answers4

1

This should fix the issue using JSONP:

$.ajax({
    url:'File Path...',
    type:'post',
    data:{uniId:uniqcod},
    dataType: 'jsonp', // use JSONP
    success: function(result){
        $('.abcClass').html(result);
        }
    });
});
NeoNexus DeMortis
  • 1,286
  • 10
  • 26
  • 2
    Please note, one of the limitations of JSONP is that it only works with GET and not POST types. – francisco.preller Apr 08 '13 at 05:48
  • I have tried this method. I am receiving response of the call. But flow is not going in success condition. Meaning i am unable to display my data. – Sid Connar Apr 08 '13 at 11:04
  • Sid, have you tried using a console.log(result) in your success to make sure you are getting the proper data? If the server is sending back JSON for example, you'll want to tell jQuery that it should expect a JSON object. – NeoNexus DeMortis Apr 08 '13 at 20:15
1

This is because of cross-domain policy. It's a security thing. I recommend you to send that request to a PHP file with cURL that is located on your server (your domain).

But you need to have cURL installed on your server: http://curl.haxx.se/ If you're using Debian based server you can do it by: sudo apt-get install php5-curl

Example:

<?php
$data = $_POST['data'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "URL FOR REQUEST");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                                                                                                                           

$result = curl_exec($ch);

echo $result;

?>
Soriyyx
  • 873
  • 3
  • 15
  • 31
0

you need to use either jsonp or cors for cross domain ajax. The code given below is an example for cors

Example code:

jQuery.support.cors = true; 

function CrosDom_ajax(url) {
        if (window.XDomainRequest
        && $.browser.msie
        && $.browser.version < 10) {
        xdr = new XDomainRequest();
        if (xdr) {
            xdr.onload = function () {
               alert(xdr.responseText);

            };
            xdr.open("get", url);
            xdr.send();
        }
        }
        else {
            $.ajax({
                url: url,
                success: function (response) {


                },
                error: function (data) {
                }
            });
         }
    }

Also you need to Write the following code in server side, to allow cross domain access

Response.AppendHeader("Access-Control-Allow-Origin", "*");           
0

Best and accepted method is to use JSONP to communicate with a different server. JSONP is a great away to get around cross-domain scripting errors.

Read the below links

What is JSONP all about?

jsonp with jquery

What are the differences between JSON and JSONP?

http://api.jquery.com/jQuery.getJSON/

Community
  • 1
  • 1
Techie
  • 44,706
  • 42
  • 157
  • 243