02-25-2011 06:44 AM
Hi All,
I'm developing a sample application which needs to establish Http connection with server running on my local machine. The requirement is to send the request to server as a JSON Object.
I tried following code for this :-
-------------------------------------------- socket.html --------------------------------------------------
<html>
<head>
<meta name="viewport" id="viewport" content="initial-scale=1.0,user-scalable=no" />
<script type="text/javascript">
var xmlHttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlHttp = new XMLHttpRequest();
}
else(window.ActiveXObject)
{
// code for IE6, IE5
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
function connect_socket()
{
alert("connect_socket....");
var url = "http://127.0.0.1:8070/?20:{\"command\":\"status\"}";
alert("opening connection");
xmlHttp.open('GET', url, true);
xmlHttp.onreadystatechange = callbackFunction;
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.send(null);
}
function callbackFunction()
{
alert("callbackFunction");
if (xmlHttp.readyState != 4)
return;
var result = xmlHttp.responseText;
alert("Result is >>>> " + result);
}
</script>
</head>
<body>
<button onClick="connect_socket()">Get Single Object</button>
<div id="results"></div>
</body>
</html>
--------------------------------------------------
The command which i need to send to server is :- 20:{"command":"status"}
where "20" is length of json object. I send it as shown in bold above.
I'm not sure whether the request is getting send successfully to server.
Can i do the same thing using socket connection in webworks?
If so, please provide useful links...!!!
03-01-2011 03:16 PM
Hi GautamPowar,
There isn't WebSockets support in WebWorks or the Browser yet. Have you made sure to whitelist your external domain in the config.xml?
<access uri="http://127.0.0.1:8070" subdomains="true" />
In order to perform a cross origin ajax call you need to whitelist the origin.