I am trying to run this piece of code taken from http://coffeescriptcookbook.com embedding it into an html.
net = require 'net'
domain = 'localhost'
port = 9001
connecting = (socket) ->
console.log "Connecting to real-time server"
connection = net.createConnection port, domain
connection.on 'connect', () ->
console.log "Opened connection to #{domain}:#{port}"
connecting connection
connection.on 'data', (data) ->
console.log "Received: #{data}"
connection.on 'end', (data) ->
console.log "Connection closed"
This code is in file named client.coffe and when i run it with the coffee command: coffee client.coffe it runs fine and connects to the server, but when I embbed it in a html file and open it i get this error: Uncaught ReferenceError: require is not defined.
My html script tags looks like this:
<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"
type="text/javascript" charset="utf-8" ></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"
type="text/javascript" charset="utf-8"></script>
<script src="{% get_static_prefix %}functions.js"
type="text/javascript" charset="utf-8"></script>
<script src="{% get_static_prefix %}jquery.dajax.core.js"
type="text/javascript" charset="utf-8"></script>
<script src="{% get_static_prefix %}client.coffee"
type="text/coffeescript" charset="utf-8"></script>
Any ideas?
This won't work in the browser.
First issue: Stuff in the browser isn't allowed to connect to other servers or ports than it's coming from for security reasons. Also, you don't get real sockets, just HTTP.
Second issue: require is a node.js command you'll only be able to use in node.js (that is, when you run a javascript file with the node command or a coffeescript file with the coffee command). The net module belongs to node.js and will never work this way in the browser.
If you want to talk to the server in realtime from inside the browser, I recommend the socket.io module which uses websockets, flashsockets and HTTP (those are usable from within the browser).
You can use require in a browser with wrappers like node-browserify. However, all problems pointed out by #thejh are correct, so you'll have to rethink your code.
Related
I'm trying to get a basic PayPal button on my web page, but I can't even load the API without getting errors. I've asked for help on PayPal's own forum, but no one replies.
Here is my page. I'm running in sandbox mode. I've replaced the actual client ID below with [CLIENTID].
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>PayPal test</title>
</head>
<body>
<script src="https://www.paypal.com/sdk/js?debug=true&client-id=[CLIENTID]"></script>
</body>
</html>
I can add a button to the page, and make a purchase, so the buying process works. But it always throws this error at page load. I'm on Win10, Brave Browser (based on Chromium).
js?debug=true&client…ruzpDLhao-FzS6:2659 unhandled_error
{err: "Error: Invalid json: .↵ at XMLHttpRequest.<anon…[CLIENTID]:2597:55)", timestamp: "1601234382102", referer: "localhost", uid: "9b7c68f416_mtk6mtc6ntq", env: "sandbox"}
env: "sandbox"
err: "Error: Invalid json: .↵ at XMLHttpRequest.<anonymous> (https://www.paypal.com/sdk/js?debug=true&client-id=[CLIENTID]:2597:55)"
referer: "localhost"
timestamp: "1601234382102"
uid: "9b7c68f416_mtk6mtc6ntq"
__proto__: Object
===============================================================
UPDATE
Sorry, I incorrectly wrote I am on Google Chrome. I'm on Brave Browser (based on Chromium). I've tested on Google Chrome and MS Edge, and it works there. Brave is where the errors happen. I've tried to add a new sandbox app, but get the same error. I've used the Brave browser settings to allow my site all privileges, but still the same error.
Live test:
Just loading API: https://www.trainerslab.app/PayPalTest.html
Example from PayPal dev site: https://www.trainerslab.app/PayPalTest2.html
Screenshot:
After using the Python http.server module I'm now trying to convert everything to Bottle and can't even get started. My problem is locating static files - everything I've tried results in a "404" error. So I've tried to reduce everything to the simplest possible example. My top level directory is in "/home/dave/test" and under that is a single file (test.py), and a subdirectory ("/home/dave/test/static") containing the single file "index.html". The html file is pretty basic:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bottle Test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Hello, World</h1>
</body>
</html>
If I double click on the file itself, it opens a new browser page and displays the "Hello, World" message. The test.py file is:
#!/usr/bin/env python3
from bottle import route, run, static_file
#route('/static/<filename>')
def server_static(filename):
return static_file(filename, root='/home/dave/test/static')
run(host='localhost', port=8080, debug=True)
If I open a browser window and enter "localhost:8080/index.html" I get back the "Error: 404 Not Found" message and the terminal window where I'm running the script looks like:
$ ./test.py
Bottle v0.12.16 server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.
127.0.0.1 - - [06/Apr/2019 15:31:25] "GET /index.html HTTP/1.1" 404 740
I've tried various permutations of the URL and root parameter but nothing I've tried works. Clearly I'm missing something very basic here. Can somebody tell me what is wrong in in the above files (or URL)?
Thanks,
Dave
I was able to contact the author of Bottle and he responded:
a route matches against the path-part of an HTTP request URL. In your
example, '/static/' would match requests to
http://localhost:8080/static/index.html and serve 'index.html' file
from the '/home/dave/test/static' directory.
"http://localhost:8080/index.html" does not match the route you
specified, thus the 404 error.
I tried this and it did indeed fix the problem.
I have a zul file running on a localserver (localhost:8080) that uses a JavaScript file stored in a separate local server (localhost:3000):
<script type="text/javascript" src="http://localhost:3000/javascript/bower_components/angular/angular.js"></script>
If I want to place the url of the separate local server in a properties file, what is the best approach for doing so? I have tried to add a placeholder:
<script type="text/javascript" src="${graph.widget.url}/javascript/bower_components/angular/angular.js"></script>
then add in the properties file:
graph.widget.url=http://localhost:3000
and then add in the zk.xml file:
<system-config>
<label-location>file:///home/asd/resources/dev-common.properties</label-location>
</system-config>
but somehow it doesn't work: it looks for http://localhost:8080/javascript/bower_components/angular/angular.js (the local server where the application runs) instead of http://localhost:3000/javascript/bower_components/angular/angular.js. What am I doing wrong?
You have to prefix the key with the labels segment.
<script
type="text/javascript"
src="${labels.graph.widget.url}/javascript/bower_components/angular/angular.js">
</script>
Source: https://www.zkoss.org/wiki/ZUML_Reference/EL_Expressions/Implicit_Objects/labels
I am using webservice to send the packets through my Perl program,
in response I am getting the following error:
<html>
<head><title>An Error Occurred</title></head>
<body>
<h1>An Error Occurred</h1>
<p>500 SSL negotiation failed: </p>
</body>
</html>
I know that I need to install one module for the ssl, my question is if I need to install the module then which module? and if there is any alternative then pl?
We'd like to have the following configuration :
one server is replying to GWT RPC : x.com (the one running Java)
another server is serving js / css / images : y.com (for bandwith optimization)
So the main page is : http://x.com/index.html
and contains this line: <script type="text/javascript" language="javascript" src="http://**x.com**/my-app.nocache.js"></script>
We're getting a SOP error: Unsafe JavaScript attempt to access frame with URL ...
Any suggestion, help about that ?
Add the following to your gwt.xml:
<add-linker name="xsiframe" />
This will generate slightly different code, that can be loaded cross-origin. Your "host page" will still have to be loaded from the same server you run your GWT-RPC servlets on, to not hit the SOP.
See this FAQ entry (the "xs" linker predates the "xsiframe" one, that latter is now preferred, and could eventually even replace the "std", default linker)
You have hit Same Origin Policy which prevents making XMLHTTPRequest to servers other than origin server. This effectively prevents cross-domain GWT-RPC.
The possible workarounds are described in Making cross-site requests:
Run a proxy on your server
Load the JSON response into a <script> tag