Tuesday, March 16, 2010

White Pages API Sample

I was looking on the web, and found that there are few if any real samples available for the White Pages People and Reverse Number Searches.

I've posted a simple example that has 2 search forms. The first one is a simple people search that takes the name and the zip code as parameters and displays the results below.

The second search was simpler, as I simply ask for a 10 digit phone number and it displays the results found below as well. You can optionally pass a state code in place of the area code, but I figured i'd keep it simple for now.

If anyone is interested in checking it out, there is source code and a working example available at:

Sample with Source.

Hope this helps someone out...

Sunday, March 14, 2010

NYTimes Json Feeds

Just getting started at this, but I'm planning on working now with the New York Times JSON API that is provided.

Looks like this is a very solid api, that allows for feeds from entertainment, to current Congress members, to much, much more...

Just signed up for my membership and requested an API by going to: NY Times Registration

A quick example that I just used for testing...

$.getJSON(
"http://api.nytimes.com/svc/news/v2/all/recent.json?api-key=XXX",
function(data) {
$.each(data.results,
function(index, item) {
var myItem = $("<ul></ul>");
$("<li></li>").html(item.headline).appendTo(myItem)
.wrap("<a target='_blank' href='" + item.url + "')></a>");
myItem.appendTo("#results");
});
});
});

Send that to a div called 'results', and you're done.

Sunday, March 7, 2010

YQL Basic JSON Yahoo News Feed

Just getting started with YQL and JSON Feeds, must say im impressed...

A quick script to pull the top stories from yahoo news in a JSON Feed

$.getJSON(
"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20rss%20where%20url%3D%22http%3A%2F%2Frss.news.yahoo.com%2Frss%2Ftopstories%22&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json&callback?",
function(data) {
$.each(data.query.results.item,
function(index, item) {
var myItem = $("

");
$("").html(item.title).attr("href", item.link).appendTo(myItem);
$("
").appendTo(myItem);
myItem.appendTo("#MYDIVID");
});
}
);

That's all it takes...

Thursday, March 4, 2010

Post Server Side without Postback

I was reading from the following article:

http://disturbedbuddha.wordpress.com/2008/01/08/executing-server-side-code-from-javascript/

and realized that this opened up some possibilities for me. Hopefully it can for you as well.
It discusses the use of combining both Javascript and Server-Side Code. I took his example a step further.
Lets say that you want to validate a user form, but you don't want to have a postback to the server during the validation. Javascript would be the preference here, and it could look similar to the following:

if
(document.formname.elementname.value == "") {
var results = document.getElementById('resultslabel');
results.innerHTML = ("Please put in a name.");
return false;
}

and the control to validate would simply be a standard html button:

<input type=button onclick=formCheck(); value=Submit />

However, let's say that you want to include a server-side event after the validation is complete. I tried first to simply add a onserverclick event handler to the button, but it posted back everytime regardless of the client status. I tried using an Update Panel, with a standard asp.net button control, same thing. This hack solved the trick. I added an ASP.NET Button, called it Button2, such as:

<asp:Button ID=Button2 runat=server style=display:none; onclick=Button2_Click />
(note the style tag...), and added a Server-side event handler to Button2.

Now, simply add the line:

document.getElementByID('Button2').click();

after the if condition in your javascript code.
Easier than I thought...

JSON Flickr Feed from Username

I needed to find a way to show flickr pictures for a Portal, and chose to hide the username in a hidden field to retrieve once it was given in registration. It's simple to use a JSON feed to retrieve a user's items if you know the User ID, but it's an extra step to acquire the User ID from the Username.

Solution: the Flickr findByUsername function
The first method requires a Flickr API Key. You can get this by going to: The App Garden. After you have signed up for an API, you can then proceed.

First, in the body, I created a hidden field called 'flickrUsername'. Then, i created a div called 'photoResults' to house the data.

Next, the jquery script:

$(function(){

var name = $('#hiddenFieldID');
if (name == ""){
$("#photoResults").html("Missing Flickr Username!");
}
else {
var userID;
$.getJSON("http://api.flickr.com/services/rest/?method=flickr.people.findByUsername&username=" + name + "&api_key=fa70b734cfb2cd4858f509230aba0328&format=json&jsoncallback=?", function(data) {
userID = data.user.id;
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=" + userID + "&lang=en-us&format=json&jsoncallback=?", function(data) {
$.each(data.items, function(i, item) {
$("<img/>").attr("src", item.media.m).appendTo("#photoResults");
});
});
});
}

This will then display an array of that particular user's images.