Accessing Web Services

From WebOS101

Jump to: navigation, search

3rd-party web service in JSON

http://www.buildasearch.com/api/v1/json/json_Parser.php

JSON API

var url = 'http://www.buildasearch.com/api/v1/json/json_Parser.php?e=' 
+ encodeURIComponent(this.model.originalValue);

Processing web service using prototype

var request = new Ajax.Request(url, {
method: 'get',
evalJSON: 'force',
onSuccess: this.gotResults.bind(this),
onFailure: this.failure.bind(this)
});
this.mybutton = this.controller.get('go_button')
this.mybutton.mojo.deactivate();
}
}

JSON parsing results

YourAssistant.prototype.gotResults = function(transport) {
var raw = transport.responseText.evalJSON(true);
var tt = raw.response.results.basresult;
 
//CODE FIX 020210 FOR SINGLE RESULTS
if (!Object.isArray(tt)){
tt= new Array(tt)
}

Setting up loop

var intro = '';
for (var i=0; i < tt.length; i++){
intro = intro + '<div><a href="'+ tt[i].showurl +'">' + tt[i].title + '</a></div><div>'
+ tt[i].basummary + '</div><div>' +tt[i].showurl+ '</div>';
 
//either "tt[i].title" or "tt.pluck('title')[i]" work above
}

Display results on stage

$('your_div').innerHTML = intro;
Personal tools