Using PARSE REST API - javascript

I am currently stuck on a task in which I have to use jQuery ajax to retrieve messages from PARSE HERE API to create something like this:
http://chatbuilder.hackreactor.com/?example=1&username=aa
I was wondering if anyone could explain to me how to use REST api to retrieve messages.
I have looked at other people's coding examples:
https://github.com/stevernator/Chatbuilder/blob/master/draft11
and
https://gist.github.com/guymorita/5726564
They seem to use ajax code like below:
$.ajax({
url: "https://api.parse.com/1/classes/chats",
dataType: "json",
success: function(data) {
var stuff = [];
for(i=0; i < 10; i++) {
stuff[i] = data.results[i].text
}
My question is: from where are the messages being generated from in this app? Do I have to create an account at Parse or would I just use ajax to retrieve this url link:"https://api.parse.com/1/classes/chats"?
Also, where would I find info on this specific class (chats)?
Any input would be greatly appreciated. Thanks!

The parse quick start guide is a very good place to start. The documentation is really good but the tutorials for the JS API are far more complex (backbone) than the documentation. I would download the Todos tutorial as there is some good stuff in it but if you dont know how backbone works its harder to read. I would have made this a comment but I don't have enough rep :(.

Related

Auto saving JAVASCRIPT data via AJAX to database

Apologies if this particular question has been solved before, I have looked everywhere it seems and can't quite get the answer I'm looking for! I am no expert and can imagine the solution is embarrassingly easy.
My problem is this: I have some php and javascript code working on a html based website, linked to a database (reading data in and also writing data out via a save function called once at the end of the script). I need the javascript code to automatically save/update itself to db via an Ajax request, without the need to keep running the page. The data being saved here needs to be read by various other pages and is relied upon to give correct results elsewhere! (so a solution would be to have the user keep the page open in the background - but suggestions for this separate issue are also welcome!)
Anyway, at the moment I have:
function sessionSave () {
var newData = kpiCA.getData().concat(kpiHA.getData(),kpiStocks.getData(),kpiCV.getData(),kpiPD.getData());
$.ajax({
url: 'saveMain.php',
type: 'POST',
data: {'kpi': newData},
success: function () {
},
error: function () {
$console.text('Data Save Error');
}
});
}
sessionSave();
I have seen some autosave scripts and the addition of timers etc. but as I am a complete noob, some help would be much appreciated,
Thanks guys!
Basically it's just timers or intervals. For example:
window.setInterval(sessionSave, NUMBER_OF_SECONDS * 1000)
// where NUMBER_OF_SECONDS is, obviously, the number of seconds to repeat your function at

AngularJS JQuery Ajax form submission Equivalent

Lately I have jumped into angularjs and its wonderful so far. I have been sending AJAX requests through $http service. Today I was shocked to find that there is no simple way to do it (to the best of my knowledge). Seaching through google and SO took me nowhere with convoluted complex solution.
Though I ended up using JQuery for now, am curious if something like JQuery ajax, capable of sending form is really available. Here is simple JQuery code to illustrate what am talking about
$.ajax({
type: "POST",
url: formUrl,
data: formData,
success: function(data)
{
$scope.form = $sce.trustAsHtml('<div class="alert alert-success">Succesfully Registred. You will be taken home in 5 seconds!</div>');
$timeout(function(){
$window.location.href= "#home";
}, 10000);
}
});
There is an equivalent in angular, here is a link to a jsfiddle with an example implementation: http://jsfiddle.net/dmcquillan314/boo5tn62/
The function on line 11 is an example of how to send a request to a rest api:
$scope.sendToResource = function() {}
To enable this feature just change the function in the directive to point to this factory and it will send a request to the api url configured in the factory.
In order to test this with an api you'll have to change the factory to match your server
here are some links to relevant documentation:
https://docs.angularjs.org/api/ngResource/service/$resource
https://docs.angularjs.org/api/ng/directive/ngSubmit
https://docs.angularjs.org/api/auto/service/$provide
Let me know if anything is unclear or you require any further explanation. Hopefully this should help you get started.

Create a Airport Database with API Access

I'm looking to create a database which I can access without api keys using a link. Essentially, I"m looking to copy this exact system: http://gd.geobytes.com/AutoCompleteCity?callback=?&q=New except I'd like to replace the results with Airport codes and names which I have. Anyone have any idea how I can do this?
Maybe it will help if I clarify further. I am using typeahead and in my custom.js file I have the following which I need to update to use my own dataset but I"m clueless on how to do this.
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 3,
limit: 8
}, {
source: function(q, cb) {
return $.ajax({
dataType: 'json',
type: 'get',
url: 'http://gd.geobytes.com/AutoCompleteCity?callback=?&q=' + q,
chache: false,
success: function(data) {
var result = [];
$.each(data, function(index, val) {
result.push({
value: val
});
});
cb(result);
}
});
}
});
After your comments:
what you want to know are three questions assuming you are gonna work with php and mysql since is easy and basic way to start with web development (imho):
you need to know:
Tutorial of how to create a database with mysql
How to connect mysql with php
How to response a json with php
How to make a ajax request
to a php server with javascript
To be honest man this is a lot to learn and if you are starting with web development I totally suggest you going for the basic first. What you are trying to build at the end is a Restful system, there a lot of frameworks and ways to do this, via Web Api C#, or http://www.slimframework.com/
as example, but again you question is too abroad and not too clear, Im trying to help you because in one moment I started with all this questions, I googled for a tutorials in web development I hope some will work for you https://www.odesk.com/blog/2014/03/10-best-web-development-tutorials-beginners/

Using JSONP API from forecast.io with WinJS

First off, apologies if this is a silly question, I'm new to WinJS and relatively new to Javascript, but I've been going great til I hit this roadblock.
I'm currently working on a weather app for Windows 8 using WinJS, and I'm trying to pull the data from the forecast.io API, which in my case is provided in JSONP format.
I easily got it working in-browser using jQuery and .getJSON, but since I have to use WinJS.xhr to request the data instead I'm having some difficulty.
I can pull the data just fine using the following code:
function getWeather() {
WinJS.xhr({
type: "GET",
url: "https://api.forecast.io/forecast/*ommited API key*/-36.044394,146.953718?callback=?&units=si",
headers: { "Content-type": "application/json" }
}).then(function complete(data) {
var weatherData = data.responseText;
(new Windows.UI.Popups.MessageDialog(weatherData, "Success!")).showAsync().done();
}, function error(data) {
(new Windows.UI.Popups.MessageDialog("Failed.", "Error")).showAsync().done();
});
}
This shows me the data in a popup window, so I'm sure that I've accessed it. The problem is, I can't do anything with it. In jQuery I simply used "data.currently.temperature" to get the current temperature data, but I can't get something similar working in this situation.
Any help or pointers would be greatly appreciated!
P.S sorry if I butchered any terminology, I'm doing my best.
Here's what data.responseText returns, there's quite a lot so I put it in a text file.
http://justadddesign.net/data.responseText.txt
It's because data.responseText is string not object. Try:
var forecastInfo = JSON.parse(data.responseText);
and then access forecastInfo object properties.

Songkick API Using JQuery

I'm having issues gathering data using JSON on the Songkick API. I'm really new to jQuery and Javascript so please forgive me if this looks really crude. I've spent some time trying to research the proper syntax I need but I just keep ending back up at square one.
What im trying to do is make the request using jQuery and then add the results into my HTML in an list. Any help anyone can give me would be fantastic.
Here's what I have so far.
<script>
$.getJSON("http://api.songkick.com/api/3.0/artists/3950031/calendar.json?apikey={apikey}",
function(data){
var events = data['resultsPage']['results']['event'];
for (var i=0;i < events.length; i++) {
$("#events").append('<li>'+events[i]['displayName']+'</li>');
}
});
</script>
Thanks!
sorry i took so long to get back, i left the office about the time you replied last night
here is what I came up with:
$(document).ready(function() {
$.ajax({
url: "http://api.songkick.com/api/3.0/artists/480448/calendar.json?apikey=APIKEY&jsoncallback=?",
dataType: "jsonp", // <== JSON-P request
success: function(data){
$.each(data["resultsPage"]["results"]["event"], function(i, entry){
$("#events").append('<li>'+entry.displayName +'</li>');
});
}
});
});
I think the issue you were having was with JSON, JSON is subject to the same origin policy (which I read about on here ( $.getJSON not working ). Instead you have to use JSON-P adding
&jsoncallback=?
at the end of the URL tells Songkick to send it as a JSON-P request. This was the main issue.
Other issues included minor coding errors: there was an extra ( in your append string, I also used $.Each as appose to a for loop but either would work, hope this helps and you may want to take your API code off if the fix worked :) Just change API Key to your key and it should be fine (i also changed the band to chevelle as the other one had no entries)

Categories

Resources