1

I am using ajax call to a remote server which is supposed to return a javascript block in response . My ajax call is something like :

    var m3_u = (location.protocol=='https:'?'https://ads.admarvel.com/fam/javascriptGetAd.php':'http://ads.admarvel.com/fam/javascriptGetAd.php');
    var m3_r = Math.floor(Math.random()*99999999999);
    var queryString = '?partner_id='+partnerId;
queryString += '&site_id='+siteId;
queryString += '&target_params=' + targetparams_str;
queryString += '&version=1.5';
queryString += '&language=javascript';
queryString += '&format=wap';
queryString += '&cb='+m3_r;

    if (window.XMLHttpRequest)
                {
                    xmlhttp=new XMLHttpRequest();
                }
                else
                {
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                alert(xmlhttp);

                xmlhttp.onreadystatechange=function()
                {
                    alert(xmlhttp.readyState);
                    if (xmlhttp.readyState==4)
                    {
                        alert("response "+xmlhttp.responseXML);
                        console.log(xmlhttp.responseXML);
                        //document.getElementById('ad').innerHTML = xmlhttp.responseText;
                        //document.write("<scr"+"ipt type='text/javascript'>");
                        //document.write(xmlhttp.responseText); 
                        //document.write("<\/scr"+"ipt>");
                    }
                }

                xmlhttp.open("GET",m3_u+queryString,false);

                xmlhttp.setRequestHeader('Content-Type','text/javascript');
                xmlhttp.send();

But the response that I am getting is null . When I hit the url in the broser , it properly returns me a javascript code block .

What is the right way to do this?

2
  • Is this a same-domain ajax call? Commented Nov 25, 2011 at 11:30
  • I'm not sure why you're setting Content-Type as a request header if you're using GET. Commented Nov 25, 2011 at 11:30

1 Answer 1

1

I think you are trying to do Cross site XMLHTTPRequest.

You have to handle Cross Site XMLHttpRequest differently. If you implement in Java you may follow below URL

http://mytechbites.blogspot.com/2009/07/cross-domain-xmlhttprequest-calls.html

Otherwise use jQuery.ajax.

Sign up to request clarification or add additional context in comments.

2 Comments

If I use JSONP , will it work for javascript response ? or do I need to return JSON format only?
JSONP is a JavaScript response - it's just that it (usually) is in the form of a one-line function call passing JSON as a parameter. So yes, it should work if you do it right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.