Yahoo Local Search Integration Part 2: Setup

In an ASPX page, I setup a simple form, as shown below.  The form simply grabs a category/zip, and submits them to the server.  But no postback occurs; rather, this kicks off a web service, which will eventually query Yahoo.

<span id="searchform">
    Category:
    <asp:TextBox ID="txtCategory" runat="server"></asp:TextBox>
    &nbsp:&nbsp;
   
    Zip Code:
    <asp:TextBox ID="txtZipCode" runat="server"></asp:TextBox>
   
    <asp:Button ID="btnSubmit" runat="server" UseSubmitBehavior="false" OnClientClick="searchClick(this);return false;"
        Text="Search" />
       
    <br />
    <hr />
    <br />
   
    <span id="searchresults"></span>
</span>

The button's searchClick method fires.  This proceeds to contact the server, and expects results from the Yahoo local search provider.  It doesn't do much in regards to validation; I didn't attempt to set up validation yet.

function searchClick(sender) {
    var category = $get("<%= txtCategory.ClientID %>").value;
    var zipCode = $get("<%= txtZipCode.ClientID %>").value;
    if (!isNaN(zipCode))
        zipCode = Number.parseInvariant(zipCode);
    else
        zipCode = null;
   
    PageMethods.GetYahooJSONForClient(category, zipCode, localSearchSucceeded, localSearchFailed, $get("searchresults"));
}

The PageMethods.GetYahooJSONForClient method is a method defined a static method in the page class, rather than going the full web service route.

public partial class yahoolocal : System.Web.UI.Page
{
    [System.Web.Services.WebMethod]
    public static string GetYahooJSONForClient(string category, int zipCode)
    {
        if (string.IsNullOrEmpty(category))
            category = "pizza";
        if (zipCode <= 0 || zipCode.ToString().Length != 5)
            zipCode = 15239;
        YahooLocalSearchProvider provider = new YahooLocalSearchProvider();
        string json = provider.GetYahooJSONFromService(category, zipCode);
        return json;
    }
}

The web service does basic validation too, and simply supplies defaults if the parameters are incorrect.  Otherwise, it calls my custom YahooLocalSearchProvider object.  This is an object I built, and is not a proxy.  The value returned is a JSON string, which the JSON string gets passed back to the client-side.

The YahooLocalSearchProvider uses the Sys.Net namespace to get a HTTP request and response, as outlined in part 1 of this series.  Because of that, I won't go into detail; here is the provider as a whole.

public class YahooLocalSearchProvider
{
    public string GetYahooJSONFromService(string category, int zipCode)
    {
        string url = "http://local.yahooapis.com/LocalSearchService/V3/localSearch?" +
        string.Format("appid=YahooDemo&query={0}&zip={1}&results=10&output=json",
        category, zipCode);

        WebRequest request = HttpWebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());

        return "var yahooLocalSearchResults = " + reader.ReadToEnd();
    }
}

Notice that the returned value sets up a search results variable.  When the eval() statement on the client-side runs, it will create a variable that contains all of the Yahoo information.  Notice how the URL is constructed here too.  All of the parameters get passed values.  THis is a simplified version of the query too, there are many more options available to the consumer.

In the next post, we'll look at how the JS contents get rendered to the client.

Published Sunday, March 29, 2009 6:32 AM by bmains
Filed under: ,

Comments

# free blog websites

Wednesday, April 21, 2010 4:25 AM by free blog websites

A Malaysian citizen by the name of Anwar Ibrahim was taken into police today, 16th of July, 2008, at about 1pm local time in Kuala Lumpur by a large team of police office that also comprise a group of police special action force, that is police commando

# http://www.graconautilus3in1carseats.com

Tuesday, May 04, 2010 8:12 PM by http://www.graconautilus3in1carseats.com

Good post I've enjoyed reading this as well as I've added your post in my personal listing of related blogs check it out www.graconautilus3in1carseats.com/.../graco-nautilus-3-in-1-car-seat-miley