0

Below is the response from server (as per google chrome rest-client):

Below is the code snippet I'm using to post some data to our server. I want to get back data as xml ouput. Keeping in mind the concept of same origin policy i uploaded this piece of code onto our sever as a html page, but not getting any response.

Is there anything wrong with my code or is my approach wrong?

<html>
<head>
<script type="text/javascript">
function getToken()
{
var xmlhttp;
var txt,x,i;


if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

alert("stage 1");
xmlhttp.onreadystatechange=function()
  {
     alert("stage 2");
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        alert("stage 3");
     xmlDoc=xmlhttp.responseXML;
     txt="";
     x=xmlDoc.getElementsByTagName("token");
     alert("stage 4");
      for (i=0;i<x.length;i++)
       {
       txt=txt + x[i].childNodes[0].nodeValue + "<br />";
       }
      document.getElementById("myDiv").innerHTML=txt;
    }


  }
 alert("stage 5");
 xmlhttp.open("POST","http://abc.mysite.com:9090/myapi/xxx",true);
 xmlhttp.send("op=login&pass=xxx");
}
</script>
</head>

<body>



<center><h2>UserPreview:</h2></center>
<br />
<div id="myDiv"></div>
<br />
 <button type="button" onclick="getToken()">GetToken</button>
 <div data-role="footer" data-position="fixed" data-theme="a"><h4>Ver: 1.1(17112)</h4>    </div>
</body>
</html>

<response><token>8768768768768768</token></response>
r4.
  • 358
  • 1
  • 6
  • 22
mrana
  • 67
  • 2
  • 12
  • 1
    why don't you use jQuery, this makes all your posting a lot easier ! – mas-designs Jan 17 '12 at 08:59
  • You can use dojo/jQuery, they have functions that using ajax for you! – TheOneTeam Jan 17 '12 at 10:20
  • @mrana, have you read my answer and try? – netadictos Jan 17 '12 at 18:16
  • @netadictos i read and tried but its not working. Not getting any response(token) from our server, code inside the if block of is not working.Is there any way to improve this by means of adding some more code inside the if block? BTW, I think should switch to jquery. Can you help me with any template code that could do the same thing while using jquery? – mrana Jan 21 '12 at 16:09
  • @mrana. At the end of my answer there is jquery code you could start with – netadictos Jan 21 '12 at 16:17
  • yeah, i saw that. will try.Thank you @netadictos – mrana Jan 21 '12 at 17:04
  • @netadictos while using jquery.ajax(), same origin policy still comes in existence or not? Pls reply... – mrana Jan 23 '12 at 11:07
  • @mrana, the origin policy applies to whatever you use, but there are other options you could use, from google proxy to more customized solutions, to avoid this policy. – netadictos Jan 23 '12 at 11:29

2 Answers2

2

You have to take into account that: for AJAX calls, you can only access the same hostname (and port / scheme) as your page was loaded from. The same domain always and the same PORT:

How exactly is the same-domain policy enforced?

In the past I use this for the IE version, I suppose for possible compatibility issues:

try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                    } 
                    catch(e) 
                    {
                        try 
                        {

                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

                        }catch(e){}

I always put the code like this:

var THEURL="http://mysite.abc.com:8080/myapi/xxx"
var data="op=login&pass=xxx";   
http.open("POST",THEURL, true);

http.onreadystatechange = function(){

};
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");   
xmlhttp.setRequestHeader("Content-length", data.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(data);

Can't you use jquery? It is much more efficient.

To make an ajax call: http://api.jquery.com/jQuery.ajax/

For example:

$.ajax({
  url: "test.html",
  context: document.body,
  success: function(){
    $(this).addClass("done");
  }
});

The sooner you use Jquery the sooner your work will get faster.

Community
  • 1
  • 1
netadictos
  • 7,602
  • 2
  • 42
  • 69
0

If the element called token in your xml file is an xml element, and not an attribute then you replace the following lines by -

txt=txt + x[i].childNodes[0].nodeValue + "<br />";

by -

txt += (window.ActiveXObject)?x[i].childNodes[0].text : x[i].childNodes[0].textContent;

I didn't ask you wheter your XHR is returning you the XML properly or not.

Acn
  • 1,010
  • 2
  • 11
  • 22