0

I want to catch cross domain response in pure javascript ajax not using jquery or other external libraries.

var xmlhttp, response;

        try
        {
            xmlhttp = new XMLHttpRequest(); 
        } 
        catch(e)
        { 
            try
            { 
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } 
            catch (e2)
            { 
                try
                {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } 
                catch (e) {}
            }
        }
      xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState==4)
            { 
                if(callback != undefined)
                { 
                   console.log(xmlhttp.responseText);

                }
            }
        }
    url = "http://xxxx.com/layer.php?callback=callRes"
     xmlhttp.open("GET", url, false);
        xmlhttp.setRequestHeader("Content-Type", "application/json,application/javascript,text/javascript");
xmlhttp.send();

Response I got

XMLHttpRequest cannot load 'http://xxxxx/layer.php?action=12&user_full_name=xyz&user_compa…e=xyz&callback=callRes'. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://test.test.com' is therefore not allowed access.
Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://xxxxx/layer.php?action=12&user_full_name=xyz&user_compa…e=xyz&callback=callRes'. 

spent lots of time and googling but not find any solution

suggestions are most welcome but not script method only pure javascript ajax

dee-see
  • 23,668
  • 5
  • 58
  • 91
Man Programmer
  • 5,300
  • 2
  • 21
  • 21
  • And does the service you're contacting support CORS, otherwise you can't do this at all. – adeneo Jul 31 '14 at 13:06
  • The `Content-Type` request header only makes sense if you are making a POST request … and only accepts a *single* content-type. – Quentin Jul 31 '14 at 13:11
  • There isn't enough information here to tell what is wrong with your code. You haven't told us what errors you get. You haven't shared details about the request or the response (which you could glean from the Net tab of your developer tools). You haven't told us anything about the data format the response is supposed to be. – Quentin Jul 31 '14 at 13:12
  • I have no idea what you even mean by "not script method only pure javascript ajax" though. – Quentin Jul 31 '14 at 13:13
  • re update, Google has lots of information about what `No 'Access-Control-Allow-Origin' header is present on the requested resource.` means. – Quentin Jul 31 '14 at 13:17
  • if you have proper ans about this, then give the ans and as you mentioned about google i'm trying google last 4-5 hrs but not getting proper ans – Man Programmer Jul 31 '14 at 13:19

0 Answers0