**UPDATE ** This is a new update to connecting to StackMob via JS. We've updated our request format with new domains and request headers. Here's the "deprecated"
connect-to-stackmob-with-JS writeup.
This example is for applications like PhoneGap whose browsers allow cross-domain ajax calls. Though, of course, this javascript would run anywhere where you can make cross domain ajax calls.
StackMob doesn't have an official JS SDK at the moment, but with a bit of JS know-how and some trust
StackMob REST API Format docs, you can create objects, fetch, and filter them through StackMob. I assume that you've already created a StackMob app.
1. Get the Javascript jsOAuth LibraryWe start by making sure you've downloaded jsOAuth, a handy JS library that let's us make secure OAuth calls. OAuth is a security protocol that we use to secure your requests to our servers so that we know that it's you making the calls. It's like when you show up at the bank and you have to enter your own PIN number to identify it's you. You're making a URL call to StackMob, but you also need to pass up your OAuth key in order to have your request authorized. Show up at the bank (URL) without a pin number (OAuth key), and you get no service. :) So, keep your keys secret please!
Here's a download to the latest version:
https://github.com/downloads/bytespider/jsOAuth/jsOAuth-1.3.3.min.js (download me!)You may find the most up to date version on the jsOAuth github page:
https://github.com/bytespider/jsOAuth (optional)2. Create a user, then retrieve that user: John SmithMake sure this page is in the same folder as jsOAuth.
Hint* Chrome doesn't normally allow cross-domain ajax requests, but Rob Griffiths, the author of jsOAuth, had this handy tip so that you can run this on your desktop to test!
You can test in
Chrome using the following commandline on
OSX /Applications/Google\
Chrome.app/Contents/MacOS/Google\ Chrome --disable-web-security
--allow-file-access-from-files --allow-file-access
--user-data-dir=~/chrome-test/ spec/runner.html
On
Ubuntu try
/opt/google/chrome/google-chrome
--disable-web-security --allow-file-access-from-files
--allow-file-access --user-data-dir=~/chrome-test/ spec/runner.html"
<html>
<head>
<script type="text/javascript" src="jsOAuth-1.3.3.min.js"></script>
</head>
<body>
<script type="text/javascript">
/* <![CDATA[ */
var stackmob, keys;
//Find your keys at
https://www.stackmob.com/platform/account/apps/reset//Set your OAuth keys (Use Development Keys)
keys = {
consumerKey: 'my StackMob public key', //Public Key
consumerSecret: 'my StackMob private key' //Private Key
};
oauth = OAuth(keys);
/*
Let's create a user instance.
Simple schemas (strings, ints, arrays, etc) are automatically generated on StackMob's end when you POST this.
Automatic Schema Generation:
http://www.stackmob.com/devcenter/docs/Automatic-Schema-Generation */
var newUser = { 'username': 'John Smith', 'password': 'opensesame', 'age': 25 };
//Make the REST API POST call to create the user
oauth.request( {
method : 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/vnd.stackmob+json; version=0' //If this was an app in production, change version=1 or version=2 as appropriate. 0 is Development
},
url : "
http://api.mob1.stackmob.com/user",
data: JSON.stringify(newUser),
success: function(data) { console.debug(newUser); },
failure: function(data) { console.error('fail'); console.debug(newUser); }
});
//Now let's fetch that user we just saved
//Let's see a list of ALL users
oauth.request({
method : 'GET',
headers: {
'Accept': 'application/vnd.stackmob+json; version=0'
},
url : "
http://api.mob1.stackmob.com/user/John%20Smith",
success: function(response) { console.debug(JSON.parse(response.text)); },
failure: function(response) { console.debug(JSON.parse(response.text)); },
});
//Let's see a list of ALL users
oauth.request({
method : 'GET',
headers: {
'Accept': 'application/vnd.stackmob+json; version=0'
},
url : "
http://api.mob1.stackmob.com/user",
success: function(response) { console.debug(JSON.parse(response.text)); },
failure: function(returnedError) { console.debug(JSON.parse(response.text)); },
});
/* ]]> */
</script>
</body>
</html>
3. More REST API CallsTo make your own REST API Calls to filter, paginate, order, look through our docs and check out the REST API Format tabs. Replicate them in your AJAX and you're good to go!