Displaying Textual Data from a remote webserver's jQuery.getJSON() script - html

I'm working on a personal project to display data from a remote server, the HTML code contains
the something like the following:
<script>
jQuery.getJSON("http://someurl.com/something.cgi?
AJZ=1&STOCK=S",
function(data) {
if (data.information[0]) {
var returnHTML = "<table style=\"border: 1px solid #E0E0E0;border-collapse: collapse;\"
It works fine on the remote server that calls the script, but I'm trying to just copy the data fields (they are 4 or 5 lines of textual data). I've attempted to write a simple perl script with use LWP::UserAgent and HTTP::Request::Common to fetch it to a html page, but the script data obviously needs a browser to execute.
The text displays fine when I visit the page, but when I try to incorporate it, I see a 'NOT AUTHORIZED'
Anyone have any tips on how to accomplish this?

Sounds like its a CORS (Cross-Origin Resource Sharing, http://www.w3.org/TR/cors/) problem, your remote server needs to allow cross domain access. Most servers are set up not to accept XMLHttpRequests from domains outside of their own, its a security measure to stop cross-domain scripting.
Or you could call a script on your local machine, a proxy script, that retrieves the data remotely for you and returns it to your javascript call.
Fetching a web page in Perl using LWP::Simple - http://www.onperl.net/Fetching-a-Web-Page-Module-LWP-Simple

Related

Call a node.js function inside an html file

This is most likely a repeated question.
The closest I got to an answer was here: execute a Nodejs script from an html page?
Yet, I still can't understand.
So here's the situation:
I have an express server setup with the following files:
Express App
server.js
index.html
and right now I want the html folder to have a button, that calls a function set in the node.js file.
Tell me if more information is needed, thanks!
EDIT:
Im remaking the question to be more clear.
I am using an express server to present a website, that present a button saying " Power Off", I want this button to be able to execute an action on my server computer, that action being a terminal command to power it off.
I wanted to know how could I make said button, written in HTML, hosted on the server but presented to the client, to interact with the server.js file hosted on the server, which would have a function set to execute said command.
thanks!
You need to understand a little better how the client/server architecture of a web page works and where code actually runs and how the client and server communicate with one another.
You can't call a function directly on your node.js server from an HTML file. The HTML file is in the client's browser. The node server is your web server, far away from the client's browser on different computers. Though it may seem like your HTML is on your node.js server because it's in a directory on that server, that's only where it is stored. When the browser requests that page, your node.js server sends the HTML to the client's browser and it is rendered back in the client browser and that's where the Javascript in that page runs (in the client's browser, far away from your node.js server). This is a client-server architecture. The HTML page is running on the client. The node.js server is on your server - different computers.
If you want to communicate with the node.js server from the web page, then you use Javascript in the HTML page to make an Ajax call from the Javascript in the web page to the node.js server (An Ajax call is an http request). You then configure a route in the node.js server for that specific Ajax call and you can then write code in node.js to do whatever you want to happen when that Ajax call is received. It can carry out some operation on the server, it can retrieve data and return it to the client, etc... You can optionally send data with the Ajax call (either as query parameters for a GET request or as body data for a POST request) and then the server can optionally return data back to you (often as JSON, but it can be any format you like).
I'm not an expert with Node, but I think what's happening here is blurring the lines between server and client. Even though both use JavaScript, there's a distinction. NodeJS could easily be replaced with Ruby, PHP, Java, whatever-backend-language-you-like, and this distinction would apply in the same way it does when you use JavaScript on the server. There's no differerence.
Server-side code executes on the server. Client side code executes on the client (the browser). If you need to call a NodeJS function (assuming it has to interact with the other server side code such as databases etc) then you can send a request, either via AJAX or standard HTTP, to the a route on the server and call that function within the route.
On the other hand, if the function is generic enough and doesn't involve any specific Node code then you can simply add a script tag with your JavaScript file to the index.html page.
There is a difference between server and client. You cannot just call a function on a server from a client directly, there is more to just that. If you wanted to, you could do it by routing a path to wherever and making an HTTP request to that path, or even, using other protocols like WebSocket if you need to communicate both ways.

POST Method not working in CGI

I am writing a CGI script with Shell as basic language and with HTML and JavaScript in it. I am using Apache server and my OS is Ubuntu Server 14.01.1.
There are multiple purposes of this script as well as it's too big so I wont be sharing it here or explaining it fully. In a nutshell, I have a form in which I am validating fields, accepting values in Shell variables and then substituting them in a shell script which also runs in a CGI file when I click submit button. Now the issue here is there are fields whose data is sensitive as a result I MUST use the POST form method.
My CGI script works perfectly fine with GET method however with POST, It just does not connect to next page. Right now, It is linked to a simple web page with no scripting or dynamic building. I instantly get the error Connection Reset By Peer. When I checked apache error log, I get the error Failed to flush CGI output to client.
I just can't find a way around this error. What can I do to make this work?

bringing in an API (xml)/URL into php for Joomla

what I need is a way to bring in this: 184.173.18.228/api/profile/1000/0/Adam.Adams (which is an xml file) into php ( Im assuming) to be used in Joomla ( I can use Sourcerer for the code in Joomla) - the problem I have been running into is that there is a cross domain proxy problem - over last weekend I was able to render that xml when it was locally on my machine using ajax/jquery:
// jQuery script
$(function() {
// Load data from proxy.php using GET request
$.get('test.xml', function(data)
{
// Search for the XML element you want, perform an action on each occurrence of found element
$(data).find("XMLElement").each(function()
{
$('#output').append($(this).attr('XMLAttribute')); // Display desired attribute of element -OR-
$('#output').append($(this).children("Phone").text());
I'm not even sure if Im asking the question correctly - what I would like to happen is: a php script to bring in that xml/url to echo/populate html on a page. I have tried a million things and just can't get it
That really looks like Javascript, not php. In php you wouldn't have the cross domain limitation anyway since it's running on the server, not the browser.
All browsers will block cross domain ajax calls.
Your solution is to write a script in php that does a wget or curl to the remote site, so you invoke a local script and the script on your server loads the remote url. You should be able to find some ready-made.

cross domain rest dojo call

I have to load data for a different url the page will run on the android browser and will load content from remote server .
I have to use dojo so I tried with dojo.xhrGet , it does'nt load the data so when I red the reference guide I saw that that method has some limitation with external url's .
So what I have to do now if I have to call a REST service with dojo .
the REST service URL on the remote server:
http://192.168.1.65:9080/RAD8JAX-RSWeb/jaxrs/customers/accounts/111-11-1111/
and the data that I should see
[{"id":"001-111001","balance":12345.67},{"id":"001-111002","balance":6543.21},{"id":"001-111003","balance":98.76}]
please help me with a method that can fix my problem I dont know dojo well I'm blocked right now because I can't use the data between two application they can't communicate
Your question is unclear, but I think you are saying you want to load data from a different domain to the main web page.
You cannot reliably use AJAX to load data from anywhere other than the domain of page you are visiting.
Almost all modern internet browsers deliberately block that, for the protection of their own users.
Instead, you will need to find some way of getting your server to relay the information.
So imagine currently you do:
Load page a.example.com/index.html
Page uses dojo to try to load b.example.com/data
You would instead need to do:
Load page a.example.com/index.html
Page uses dojo to try to load a.example.com/data
When the a.example.com server receives a request to load /data, it should connect (perhaps using cURL) to b.example.com/data and then output the same text.

call nodejs scripts from html pages

I would like to call node.js scripts like i do with php, that is simply using their url.
I'm mainly a js programmer, so it would be wonderful for me to ditch out php totally and use node for the server side scripting.
But most of the tutorial i saw until now involved creating a server, etc...
I just want to call a script in the old way, like www.mysite.com/login.js instead of www.mysite.com/login.php
And once called the script should return or a page for rendering or simply json or other text for ajax calls.
Is it possible?
There's another possibility, similar to the CGI method mentioned above but using a module (https://github.com/samcday/node-fastcgi-application) in Node.js that supports FastCGI which would allow Apache to talk to it.
It actually blends both styles, so that the Node program is launched automatically by Apache but stays around as long as there are requests to process. You simply set up a rule to redirect the pages you want to a dispatch.njs script, which you have added with AddType in .htaccess as a Node script, which launches and then handles requests on the stdin and sends the results to stdout. But you still need the routing provided by express because it's only looking at HTTP_REQUEST_URI to determine what page you want.
Another option would be to setup Node listening on a certain port and proxy requests to it from Apache if they match a certain signature (like ends in .njs).
But most of the tutorial i saw until now involved creating a server, etc...
I just want to call a script in the old way, like www.mysite.com/login.js instead of www.mysite.com/login.php
That's how node.js works. You create a server. With PHP the first step of a tutorial is to install and setup apache (creating the server).
The equivelant of your question in PHP terms would be
Can I run PHP scripts without installing apache/nginx/other webserver
Which you can't (I believe recent or future version include a web server baked in, just like node.js !)
You need to install node.js, you need to tell node to run a web server
However you can use expressjs for a more streamlined and familiar setup. You can then just call express on the command line to scaffold your server out.
You still have to install node.js (and npm)
Node.js and PHP are two different things.
Node.js is an "event-driven I/O server-side JavaScript environment". When it functions, Javascript is not run as a scripting language, it is processed just like Ruby or Python. You start a server, and the code is run.
PHP, however, is run as a scripting language on a webserver, because the web server has a PHP processor module installed on it. Therefore, you can run PHP scripts directly by the .php extension, because the Apache server is configured to interpret .php files as scripts.
In other words, what you'd like to do is not possible without a large amount of hacky tricks, with node.js.
However, if you'd like to just use JavaScript instead of PHP, I'd check out JS-CGI, which allows you to use Javascript as a CGI extension.
You could use CGI. Something like this:
#!/usr/local/bin/node
var sys=require("sys");
sys.puts("Content-type: text/html\n");
sys.puts("Hello World!<br/>\n");
var argstr="";
for(var i in process.env){
argstr+=i+": " + process.env[i] + "<br/>\n";
}
sys.puts("args: "+ argstr +"<br/>\n");
Just like Perl/Python/../..