Trying to fetch json string from internet api - javascript

I am trying to fetch the json string from this url:
http://status.mojang.com/check
however every time i get a failure or this:
XMLHttpRequest cannot load http://status.mojang.com/check. Origin null is not allowed by Access-Control-Allow-Origin.
Any help appreciated
<!DOCTYPE html> <html> <head> <title>mc stats</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script> <meta http-equiv="Access-Control-Allow-Origin" content="*"> </head> <body>
<div id="minecraft"> </div>
<script> (function() { var flickerAPI = "http://status.mojang.com/check?jsoncallback=?"; $.getJSON(flickerAPI)
.done(function( data ) {
console.log(data); }); })(); </script> </body> </html>

You need to run the webpage in a server and this problem should go away. There might be a way to do it with Google Chrome, but I am not aware of it. When I have this issue, I typically run my file with Aptana Studio. It is a free download and it causes the file to be served up from a server instead of just being read from your file system. It defaults to Firefox, but you can just paste the url in Chrome and everything should work like you expect, unless there are other errors.
If you are running a mac/linux you also have access to the python simple server and it should serve it up also. Run the command python -m SimpleHTTPServer from the file directory and you should be able to see it at localhost:8000. Good Luck!

Related

Python CGI isn't doing anything. Did I misconfigure something?

I'm trying to learn web dev, and I've run into a problem. This feels like it ought to be dead simple, and yet, here I am.
I want a Javascript function on a webpage to call a Python script on the server and log the result.
I'd guess I probably configured my server wrong in some way. For reference, I'm using the Bitnami LAMPP server on Amazon Lightsail.
As far as I know, I configured Apache correctly for CGI. In httpd.conf I already have the ScriptAlias for /cgi-bin/, I've enabled mod_cgid, I've added +ExecCGI to the options for the directory and added the cgi-script handler.
In my cgi-bin folder, I have cgi.py:
#!usr/bin/env python
import cgi
print("Hey, look, the CGI worked")
and in my htdocs folder I have /cgitest/index.html:
<!DOCTYPE html>
<html>
<head>
<title>CGI</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
Check the console
<script>
$( document ).ready(function() {
console.log( "ready!" );
$.get("/cgi-bin/cgi.py", function(data){
console.log(data);
});
});
</script>
</body>
</html>
But all I see in the console is "ready!"
Other tests reveal that it's never getting into the function in get().
I'm wondering whether I've configured something on the server wrong, or whether there's something wrong with my code.
Update: Got the status code for my request. I'm getting 403 Forbidden when cgi.py is in the same directory as the webpage, and 500 Internal Server Error in the /cgi-bin directory.

How to load commonmark.js in a local HTML file?

I was following the steps and wrote a demo HTML. Here are the steps in the README of https://github.com/commonmark/commonmark.js#commonmarkjs :
For client-side use, you can do make dist to produce
a standalone JavaScript file js/dist/commonmark.js,
suitable for linking into a web page, or fetch the latest
from
https://raw.githubusercontent.com/jgm/commonmark.js/master/dist/commonmark.js,
or bower install commonmark.
Here is my demo HTML.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://raw.githubusercontent.com/jgm/commonmark.js/master/dist/commonmark.js"></script>
<script>
window.onload = function() {
console.log(commonmark)
}
</script>
<body></body>
</html>
Here is a JSFiddle URL for my demo: https://jsfiddle.net/y3xohp7x/
I saved this HTML locally in a file named foo.html and opened this local file with Firefox 55.0.1.
But if I load it with Firefox 55.0.1, I get the following errors in the console.
Loading failed for the <script> with source “https://raw.githubusercontent.com/jgm/commonmark.js/master/dist/commonmark.js”. foo.html:5
ReferenceError: commonmark is not defined foo.html:9:5
Questions:
Why does this error occur?
How can I resolve this error without having to copy commonmark.js to the local filesystem?
Is it a bug in the commonmark.js README documentation that I quoted above or is it an error in my understanding of the documentation?
Edit
Actually, there apparently is a way to do this for github content in particular: https://rawgit.com/
This website gives you a link to a version of the script that will be served with the correct MIME-type.
So this should load the script properly for you:
<script type="application/javascript" src="https://cdn.rawgit.com/jgm/commonmark.js/master/dist/commonmark.js"></script>
https://jsfiddle.net/w1uvq59r/
Original Answer
The reason is that the script source is sent back with the headers:
"Content-Type": "text/plain"
"X-Content-Type-Options": "nosniff"
The latter header prevents the browser from executing the script due to the fact that the content-type is not executable, like "application/javascript". Which means unfortunately there is really no way to get the script to load remotely. Here is a thread with more information on a similar problem.
The only solution, as far as I can tell, is to load it locally, like so:
<script type="application/javascript" src="path/to/the/file.js"></script>

Resource requests from webpages fail, but direct request work

I've installed CentOS and LAMP on an old desktop at home to practice system administration and webadmining, but I'm running into a strange problem.
I have a webpage that's no more than:
<!DOCTYPE html>
<html
<head>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
</body>
</html>
The chrome console is telling me that the request for test.js is failing. When I right click the link it gives and open it in a new tab, it correctly shows.
Is there a setting in apache that I need to set for this to work?
edit: Only thing in the logs is about the favicon
edit:
Some additional details: I'm accessing via the local ip of the machine, external script and stylesheet requests work, and all the files are in the same directory.
edit: Using fiddler, it looks like chrome isn't even sending out the request for the files.
edit: The response headers for the webpage include "Connection: close"
Make sure that file with that HTML code, and test.js are in the same directory.
I suspect that's the problem.
If you double click file on your system it's not opened via apache.
If you are opening html by
http://localhost/test.html
Then chrome will search to test.js in
http://localhost/test.js

javascript - How to properly initialize timezone-js

Im trying to learn the timezone-js I got from here.
I think I followed the instructions on how to get started. I downloaded all the necessary files including the Olson files but Im getting this error when I check my firefox console: Error: Error: Error retrieving "../files/tz/northamerica" zoneinfo files.
My code is (jtimezone.html):
<html>
<head>
<script type="text/javascript" src="../javascript/jquery/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../javascript/timezone-js/src/date.js"></script>
<script type="text/javascript">
$(document).ready(function () {
timezoneJS.timezone.zoneFileBasePath = "../files/tz";
timezoneJS.timezone.defaultZoneFile = "asia";
timezoneJS.timezone.init();
var dt = new timezoneJS.Date('10/31/2008', 'America/New_York');
alert(dt);
});
</script>
</head>
<body>
</body>
</html>
I think I have set the file path for the zoneFileBasePath. Anyway my html is located at /home/idiotboy/Documents/timzone_test/html. The downloaded timezone-js is at /home/idiotboy/Documents/timzone_test/javascript. The tz files are at /home/idiotboy/Documents/timzone_test/files.
Please help. If you can suggest a better/easier way to handle timezones in javascipt please do. Im still a beginner. Thanks.
Browsers don't allow AJAX to access local filesystem. You'll need to serve you page through Apache or Nginx and open your html with the URL localhost or 127.0.0.1

How to run another program from HTML

I have the following snippet of the code:
<html>
<head>
<title>Example of Very First PHP Script ever!</title>
</head>
<body>
<script>
function comexe(){
C:\Program Files\Notepad++\notepad++.exe C:\Documents and Settings\User\Desktop\file.txt
}
</script>
music
</body>
</html>
How can I run another program from the HTML? What am I doing wrong?
Adding this as an answer, as requested in the comments
You can't - HTML is client side, and javascript is sandboxed to protect the client PC from exactly this. Apart from the title, your script has nothing to do with PHP - which can run a filesystem command, but only on the server.
Technically you could execute a program from inline vbscript. You would normally do this in a .hta file running locally.
Something like:
<html>
<head>
<title>Example of Very First PHP Script ever!</title>
</head>
<body>
<script language="VBScript">
Sub RunProgram
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "C:\Program Files\Notepad++\notepad++.exe C:\Documents and Settings\User\Desktop\file.txt"
End Sub
</script>
music
</body>
</html>
You can not do that becasue html and javascript both client side tech. and have restriction so that it can not access resource of the client machine.
If it's a operation that you have to perform many times on client machine this could be achieved.
Just install a web server (wamp or xamp or what you want on machine) and from your page call
http://localhost/nameofscript.php
this script can execute whatever you want if you set correct permissions on the user of local webserver
Perhaps, if is one-time operation that a client must execute is not so pratic to install a webserver only for an onetime operation
Bertoli Stefano

Categories

Resources