Calling an ASHX handler from JavaScript

posted in: Uncategorized | 0

In my PhoneGap project I want to grab some data to display on Bing Maps.  Originally I created myself a really neat rest formatted WCF service that return JSON, but after I spent a day pulling my hair out trying to deploy it in IIS I gave up and went for something uber simple. Over on my server I’ve created myself an ashx handler and configured the urlmappings etc. so it looks quite neat and returns a nice JSON data packet.

This is the way I decided to access it from JavaScript – seemed the simplest.

        function callASHX(latitude, longitude) {
            var request = new XMLHttpRequest;
            request.open('GET', 'http://mysite.com/Items.aspx?latitude=' + latitude + '&longitude=' + longitude + '&buffer=500', false);
            request.send();
            if (request.status === 200) {
                var result = JSON.parse(request.responseText);
                if (result == null || result.Items == null || result.Items.length == 0) {
                    alert('No Items here');  
                } 
                 else {
                    addItems(result,latitude, longitude);
                }
            }              
        }