I'm quite newbee on API thing, been reading the documentation here for JAVASCRIPT client but I can't make things work, even on authentication part. I already have the client ID and ClientSecret from PODIO itself.
Basically, I want to get all podio data in a workspace in a JSON format using client side (browser only).
I've downloaded the library here and created an HTML file on my localhost and link the podio-js with following code. Getting this error "podio-js.js:1 Uncaught ReferenceError: require is not defined at podio-js.js:1". Do I need to install something such that loader thing to make this work?
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="lib/podio-js.js"></script>
<script type="text/javascript">
var podio = new PodioJS({
authType: 'server',
clientId: 'foo',
clientSecret: 'foo'
});
var redirectURL = 'http://localhost/PODIO-JS/podio-js-master/PODIO_CLIENT.html';
// Your request handler (for example in ExpressJS)
var action = function(request, response) {
var authCode = request.query.code;
var errorCode = request.query.error;
podio.isAuthenticated().then(function() {
// Ready to make API calls...
}).catch(function(err) {
if (typeof authCode !== 'undefined') {
podio.getAccessToken(authCode, redirectURL, function(err, response) {
// make API calls here
console.log (responsedata);
});
} else if (typeof errorCode !== 'undefined') {
// a problem occured
console.log(request.query.error_description);
} else {
// start authentication via link or redirect
console.log(podio.getAuthorizationURL(redirectURL));
}
});
</script>
</head>
<body>
</body>
</html>
You can only use the syntax PodioJS = require('podio-js') if you're working in an AMD environment, typically using requirejs.
You're using a good ol' HTML page instead, which means you have to follow the second part of the browser usage section found here: https://github.com/podio/podio-js#browser
From within the podio-js folder:
npm install -g browserify
npm run bundle
and then include dist/podio-js.js using a tag in your HTML page.
Note: once you've bundled the source, you can copy paste the compiled file wherever you want.
Related
How do you return a function from an express server and run it in a browser?
I have hosted a function on Google Cloud Platform (Cloud Functions) and want to run it in a web application via a script tag. The function is in Nodejs 16 and can be called via https, and is a simple hello world example:
exports.helloWorld = () => {
const widget = console.log("This is a widget");
return widget();
}
However, it seems as though I cannot call it that way, because I get an Error: could not handle the request and a 500 status message each time I try to hit the endpoint.
I want to be able to embed this function on a website via something like:
<script src="https://example.com/function-name"/>
How can I achieve that?
It seems that your Cloud Function is malformed. When you create a Cloud Function you have to return a status code otherwise the Cloud Function will timeout waiting for the return status code (throwing the 500 error you had).
If I guess correctly what you want to do is call an external script from the Cloud Function, you can use the next code:
exports.helloWorld = (req, res) => {
let message = 'alert("Hello world from CF")'; //Or any js code script here
res.status(200).send(message);
};
And call it from the HTML page like this:
<!DOCTYPE html>
<html>
<head>
<script src="https://zone-project-id.cloudfunctions.net/helloworld"></script>
</head>
<body>
<!-- the content goes here -->
Hello world!
</body>
</html>
I'm using a Javascript file to try and get a token from ArcGIS Online. However, whenever I try it, it comes back with
init.js:11 Uncaught Error: undefinedModule
The file (GetAToken.js) is below:
dojo.ready(init);
var request = dojo.require('request'); // npm install request
// generate a token with your client id and client secret
function getToken(callback) {
request.post({
url: 'https://www.arcgis.com/sharing/rest/oauth2/token/',
json: true,
form: {
'f': 'json',
'client_id': '<<MY_CLIENT_ID>>',
'client_secret': '<<MY_CLIENT_SECRET>>',
'grant_type': 'client_credentials',
'expiration': '1440'
}
}, function (error, response, body) {
console.log(body.access_token);
callback(body.access_token);
});
}
And the bit which calls it (in a HTML file) is:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://esri.github.io/calcite-bootstrap/assets/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://js.arcgis.com/4.0/esri/css/main.css">
<script src="https://js.arcgis.com/4.0/"></script>
<script src="GetAToken.js">
var MyToken = callback(getToken);
alert(MyToken);
</script>
It looks like you are trying to get the requestJS through NodeJS (npm install request). I am right?
You need to be aware that NodeJS require uses CommonJS whereas dojo uses RequireJS. Both of them have different module structures in them. More details here
In the line var request = dojo.require('request'); It is not able to find request module so it is throwing the error.
The way to get nodejs modules in dojo is to use dojo/node as show below.
require([ "dojo/node!request" ], function(request){
// Utilise the "request" module
});
Go through the Tutorial for Dojo and Node.js
Hope this was helpful.
PS: Esri has its own request object (esri/request), which you can use to get tokens. You may want to use that instead.
I am trying to use SignalR with cross domain but i am getting error message when calling start function. Error message is "Uncaught TypeError: Cannot call method 'start' of undefined "
I am using code
Server side:
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
EnableJSONP = true
};
map.RunSignalR(hubConfiguration);
});
}
}
}
Client side code.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="/Scripts/jquery-1.6.4.min.js"></script>
<script src="/Scripts/jquery.signalR-1.1.4.min.js"></script>
</head>
<body>
<div></div>
<script type="text/javascript">
var connection = $.connection.hub.url ='http://localhost:9370/signalr';
connection.hub.start()
.done(function () {
alert('Now connected, connection ID=' + connection.id);
});
</script>
</body>
</html>
There are problems with the initialization and start of your Signalr connection, also declare a proxy to reference the hub. See below example:
<script src="/Scripts/jquery-1.6.4.min.js"></script>
<script src="/Scripts/jquery.signalR-1.1.4.min.js"></script>
<script src="http://localhost:9370/signalr/hubs"></script>
<script type="text/javascript">
$.connection.hub.url ='http://localhost:9370/signalr';
var yourHubProxy = $.connection.YourHubName;
//Do something here with yourHubProxy
$.connection.hub.start().done(function () {
alert('Now connected, connection ID=' + $.connection.hub.id);
});
</script>
Another thing, I'm not sure why you're using different versions of SignalR in your server side and client side. To me you had SignalR 2.x on your server side and SignalR 1.1.4 on your cient side.
Take a look at the following link, it's good an example about SignalR with cross domain.
http://damienbod.wordpress.com/2013/11/01/signalr-messaging-with-console-server-and-client-web-client-wpf-client/
#Lin has already answered the question , but i want to inform one important point related to CROSS DOMAIN connection.
Mostly when you googled this issue , we used to find all examples using localhost for ex: http://localhost:9370
As you're binding to localhost only , so it will not work if you are trying to access from other address like http://dev-domain:9370/signalr/hubs remotely you will get HTTP Error 400 i.e the request hostname is invalid.
In order to bind all addresses on the machine ,we have to use it like this
http://*:8097
If anyone found issue after this , check the firewall please :)
I'm having an html file with a hyperlink which calls javascript function.The javascript function has to call a batch file...this all should happen from Node.js
<html>
<head>
<title>sample</title>
<script src="child.js"></script>
</head>
<body>
click here
</body>
</html>
child.js
function call()
{
var spawn = require('child_process').spawn,
ls = spawn('append.bat');
}
I'm getting error like this....
ReferenceError: require is not defined
var spawn = require('child_process').spawn,
any answer..pls reply...
Node.js is a server-side environment for JavaScript. To interact with it from a web page, you'll want to establish an http.Server and use Ajax to communicate between.
A partial example (using a few libraries to simplify) would be:
// server-side
app.post('/append', function (req, res) {
exec('appand.bat', function (err, stdout, stderr) {
if (err || stderr.length) {
res.send(500, arguments);
} else {
res.send(stdout);
}
});
});
// client-side
function call() {
$.post('/append').done(function (ls) {
console.log(ls);
}).fail(function (xhr) {
console.error(xhr.responseText);
});
}
The libraries demonstrated are Express for server-side and jQuery for client-side. It also uses child_process.exec() rather than spawn() to get Buffers rather than Streams.
Resources:
Learn jQuery
Express Guide
SO's node.js Tag Info, which includes a number of "Tutorials, Guides and Books" and "Free Node.js Books and Resources."
You can't access Node.js from browser.
I'm trying to put together a simple "Hello World" style application with SignalR. The slightly complicating factor is that the SignalR hubs need to be self-hosted, not in IIS/ASP.NET. I've got the server-side working, so far as I can tell, and it's available on port 8080, but I'm having trouble wiring up the client. The problem I'm bumping up against at the moment is that the SignalR client seems to be ignoring the port on the URL that I specify.
Specifically, I've got this code here:
<head runat="server">
<script type="text/javascript" src="/Scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="/Scripts/json2.min.js"></script>
<script type="text/javascript" src="/Scripts/jquery.signalR-0.5.3.js"></script>
<script type="text/javascript" src="http://<%=Request.Url.Host %>:8080/signalr/hubs"></script>
<title>SignalR Test</title>
</head>
<body>
<script type="text/javascript">
$(function () {
// Wire up the client to the SignalR server on the same host
// as the source of this page, but on port 8080.
$.connection.url = "http://<%=Request.Url.Host %>:8080/signalr";
var roomHub = $.connection.roomHub;
$('#echoButton').click(function () {
roomHub.echo($('#echoButton').val())
.error(function (err) {
alert(err);
});
});
$.connection.hub.start({ transport: 'auto', xdomain: true })
.done(function () {
console.log('Connected.');
})
.fail(function (e) {
console.log('Unable to connect:' + e);
});
});
</script>
The :8080/signalr/hubs script loads successfully, and it looks good, i.e., it has the definition for the roomHub in it, so I know that the server is up and running.
However, when $.connection.hub.start() runs, it seems like it should try to open up a connection for a URL something like http://app.dev.alanta.com:8080/signalr/signalr/negotiate?=1353072553935. Instead, Firebug tells me that it's ignoring the 8080 portion, and is instead trying to negotiate a connection with the URL http://app.dev.alanta.com/signalr/signalr/negotiate?=1353072553935. And of course, that doesn't work - there's no SignalR service listening on port 80, just the regular web server - so it fails with the message, "Unable to connect:SignalR: Error during negotiation request".
I should also note that in the jquery.signalR-0.5.3.js file, the bit of code that parses out the connection does indeed seem to ignore the port:
// Resolve the full url
parser.href = connection.url;
if (!parser.protocol || parser.protocol === ":") {
connection.protocol = window.document.location.protocol;
connection.host = window.document.location.host;
connection.baseUrl = connection.protocol + "//" + connection.host;
}
else {
connection.protocol = parser.protocol;
connection.host = parser.host;
connection.baseUrl = parser.protocol + "//" + parser.host;
}
// Set the websocket protocol
connection.wsProtocol = connection.protocol === "https:" ? "wss://" : "ws://";
Is this a bug? Or have I misunderstood something?
Well, I could swear that I'd tried this and it didn't work, but as I was troubleshooting it some more, I changed the URL assignment from this:
$.connection.url = "http://<%=Request.Url.Host %>:8080/signalr";
To this:
$.connection.hub.url = "http://<%=Request.Url.Host %>:8080/signalr";
And to be sure, this is how it's documented here. I thought that I'd seen it documented the first way somewhere, but I can't find it now. Oh well. Chalk this one up to my not paying enough attention.