rest - Javascript code using xhr is not working? -



rest - Javascript code using xhr is not working? -

below response server (as per google chrome rest-client):

below code snippet i'm using post info our server. want info xml ouput. keeping in mind concept of same origin policy uploaded piece of code onto our sever html page, not getting response.

is there wrong code or approach wrong?

<html> <head> <script type="text/javascript"> function gettoken() { var xmlhttp; var txt,x,i; if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code 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"); (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>

you have take business relationship that: ajax calls, can access same hostname (and port / scheme) page loaded from. same domain , same port:

how same-domain policy enforced?

in past utilize ie version, suppose possible compatibility issues:

try { xmlhttp = new activexobject("msxml2.xmlhttp"); } catch(e) { seek { xmlhttp = new activexobject("microsoft.xmlhttp"); }catch(e){}

i set code 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 utilize jquery? much more efficient.

to create ajax call: http://api.jquery.com/jquery.ajax/

for example:

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

the sooner utilize jquery sooner work faster.

javascript rest

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -