Hit URL in local VM machine via AJAX/REST - json

I am new to API methodology so sorry for asking such silly question which it may be.I am running Windows 10 on my machine and have setup a Ubuntu VM on Oracle VM VirtualBox. I want to hit the url in AJAX/REST way to get the JSON.I am able to get that JSON data in browser when I am hitting the url within VM environment. How can I accomplish the same via desktop?
I am running a separate server utility on VM which accepts the following url
localhost:8998/sessions
and it provides me data in form of JSON.
I have written the following code with help of W3Schools.
<!DOCTYPE html>
<html>
<body>
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "192.168.56.780:8998/sessions", true);
xhttp.send();
}
</script>
</body>
</html>
This should return json file in output in the same way as that when I run in browser in VM environment but no output is there on it in specified section.
UPDATE:
I have made changes in "Network" settings of VM making Adapter 2 as below and started the image:
Now I am able to putty it through new IP address 192.168.56.780 assigned automatically by it.Will this solve the above problem.

Related

XHR works on localhost server but not on my static IP on IIS

I am using IIS on my Windows Server 2012 R2 program. I have a static IP, and I've binded it to a port and enabled port forwarding so it is accessible online. Here's the thing, on localhost my XHR request produces the desired output. But when I search my static IP in the URL, and attempt the request, it does not produce anything. Here are the files:
<!DOCTYPE html>
<html>
<body>
<h1>The XMLHttpRequest Object</h1>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "demo_get.txt", true);
xhttp.send();
}
</script>
</body>
</html>
And here's my desired output "demo_get.txt":
<p>This Content was requested using the GET method.</p>
So can someone please guide me in making my XHR request functional when I enter my Static IP into the url? Thanks.
As suggested by others, I went into the Developer Tools and found that no request was being sent on the Network Tab. So, I investigated my security settings on my local intranet. I enabled some settings, and that's all it took for the XHR requests to function. Thank you everyone for your input!

WebSocket API Connection issue

I suppose I'm havin' a beginner's problem regarding websocket connection.
Here's the thing, I'm researching on using websocket and I followed this code:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function WebSocketTest()
{
if ("WebSocket" in window)
{
alert("WebSocket is supported by your Browser!");
// Let us open a web socket
var ws = new WebSocket("ws://172.16.0.195:8080/echo");
ws.onopen = function()
{
// Web Socket is connected, send data using send()
ws.send("Message to send");
alert("Message is sent...");
};
ws.onmessage = function (evt)
{
var received_msg = evt.data;
alert("Message is received...");
};
ws.onclose = function()
{
// websocket is closed.
alert("Connection is closed...");
};
}
else
{
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
</script>
</head>
<body>
<div id="sse">
Run WebSocket
</div>
</body>
</html>
... which I copied from the internet. When I tested it, Firefox said that my browser supports websocket feature but it DOESN'T ESTABLISH CONNECTION TO MY SERVER. I try to telnet our server in port 80 and I could gain connection to it. But if I am to use websocket connection in my page,it does not establish connection anymore.
Thank you very much in advance for any inputs/help.
[EDIT]
** by the way, does ".../echo" in my specified websocket connection has to do something with it? I mean, do I have to have some echo.php in my server?
This tutorial helped me a lot:
Getting started with HTML5′s Web Sockets
Now I have my server set up already.

xmlhttprequest always returning status 0 even client and server on same machine

i know this questions asked several times, and i am referring all these post, even after that also not able to solve my problem. I have created a html page for client server communication. Here is the code
<!DOCTYPE html>
<html>
<head>
<title>Sandbox</title>
<script type="text/javascript">
function log (text) {
document.getElementById("contents").innerHTML = document.getElementById("contents").innerHTML + "<br />" + text;
}
function ready() {
log("Ready.");
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
log("State: " + xmlhttp.readyState + ", Status: " + xmlhttp.status
+ ", Statustext: " + xmlhttp.responseText);
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
log("CSV Content:");
log(xmlhttp.responseText);
}
};
log("Open.");
xmlhttp.open("GET", "http://10.5.13.142/iptvservice.xml", false);
log("Send.");
xmlhttp.send(null);
log("Sent.");
window.removeEventListener('DOMContentLoaded', ready, false);
}
window.addEventListener('DOMContentLoaded', ready, false);
</script>
</head>
<body>
<div id="contents">Loading.</div>
</body>
</html>
server is a Apache server.I am running this page on a same machine where server installed. On Mozilla status code is 0 and on It hanged on loading. I am not getting what is the problem. i have read that you don't need to set the permission on manifest.json if you are on the same domain. Then where i am getting wrong. Please help.
Edit: Actually my requirement is to run this code on android using phonegap. So i want to do using java script. So anybody can suggest using xmlhttprequest how to create client server connection.
sorry, but just now i got this link
XMLHttpRequest Fails On Same Domain
but in my case Apache server giving xml page. So where i should put my script.and this is just for testing purpose i am using same machine, but after that i need to run same page on different machine. Then what would be the solution. sorry i am asking very simple question, but required little help.
Edit: just for information. I did change according to the link
http://www.skill-guru.com/blog/2011/02/04/adding-access-control-allow-origin-to-server-for-cross-domain-scripting/
and then ran on google chrome, it worked, but it still not working on firefox. Anyways, atleast my code and server installation is proper.

Chrome addon with my server interaction

its many days reading hundreds of ways to help me make what I really need. No success at all.
What I need is this:
1) Having a button which only works when the tab has a certain url.
2) After clicking it, must read page's source and then get some pieces of it to send them to my server page in order to check my database for recordcounts (I assume with AJAX & javascript). Then this page should send back to the extension its responses and populate the popup html.
Looks easy I know, but please I need the workflow if not the required codes for the extension.
Thank you so much!
ok so you can chceck selected tab and it's url with:
chrome.tabs.getSelected(null,function(tab) {
workWithUrl(tab.url);
});
...
function workWithUrl(url){
if (url == ...
...
}
To be able to chceck this you need to add permission for "tabs"
To process page source code, send it to web service and change popup.html:
var xhr = new XMLHttpRequest();
xhr.open("POST", "__server adress___", true);
//headers
xhr.setRequestHeader("Content-Type", "application/json");
//response
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
//response from service to popup.html
document.body.innerHTML = xhr.responseText;
}
}
//process page here
xhr.send(pageText);
You have to add permission for server adress to manifest as well and everything should be executed from popup.js (or html).

Chrome Extension: How to List The Contents of Local Directory

What I want to do:
I want to create a Chrome extension for test purposes. And that extension should be able to access the directories and files on the local hard drive.
How I did It:
I requested permissions for file:///* in my manifest.json file. Then I made sure that the checkbox Allow access to file URLs was checked. Then I made a XHR for a file:
var xhr = new XMLHttpRequest();
xhr.open("GET", 'file://c:/dir/file.name', true);
xhr.onload = function(e) {
console.log('response: ', xhr.response);
}
xhr.send();
... and a XHR for a whole directory:
var xhr = new XMLHttpRequest();
xhr.open("GET", 'file://c:/dir/', true);
xhr.onload = function(e) {
console.log('response: ', xhr.response);
}
xhr.send();
... and for the total file system:
var xhr = new XMLHttpRequest();
xhr.open("GET", 'file:///', true);
xhr.onload = function(e) {
console.log('response: ', xhr.response);
}
xhr.send();
What happened:
I was able to get the right response for my file request.
When I requested a directory I received the source code of Chrome's
default human-readable directory overview:
.
<html>
<head>
<script>
[...]
</script>
<style>
[...]
</style>
<title id="title"></title>
</head>
<body>
<div id="listingParsingErrorBox" i18n-values=".innerHTML:listingParsingErrorBoxText"></div>
<span id="parentDirText" style="display:none" i18n-content="parentDirText"></span>
<h1 id="header" i18n-content="header"></h1>
<table id="table">
<tr class="header">
<td i18n-content="headerName"></td>
<td class="detailsColumn" i18n-content="headerSize"></td>
<td class="detailsColumn" i18n-content="headerDateModified"></td>
</tr>
</table>
</body>
</html>
[...]
<script>start("C:\\dir\\");</script>
<script>addRow("..","..",1,"0 B","11/11/11 11:11:11 AM");</script>
<script>addRow("file.name","file.name",0,"4.9 kB","11/11/11 11:11:11 PM");</script>
And when I requested the whole filesystem, I got an error.
My Question:
I'm able to read simple files. But how can my extension get a nice machine-readable overview of a directory or of the total filesystem? Thanks.
EDIT: I know that the FileSystem API is intended to access the sandboxed filesystem://, but maybe Chrome allows extensions to also access file://. I couldn't find any documentation concerning file:// access from Chrome extensions, so I'm just able to guess.
There's no built-in feature in stand-alone Chrome.
Alternatives to parsing the directory listing page:
NPAPI extensions (C++, recommended)
a local server with filesystem access. NodeJS is a good pick.