0

I'm doing the following - it's a JQuery .get() that calls an http handler that should return a JSON object. In the callback function for the .get() (i.e. the third argument passed in the .get() function), which is called when a successful http response is received, the Latitude and Longitude properties of that object are then assigned to a couple of other properties

    var url = "/handlers/GeolocationByIpAddress.ashx";
    $.get(url, {}, function (data) {
        g.currentLat = data.Latitude;
        g.currentLng = data.Longitude;
    });

When I set a breakpoint on the line:

        g.currentLat = data.Latitude;

It isn't hit, which suggests that the success callback isn't triggering. I've checked the request to the .ashx handler in the 'NET' tab of Firebug, and it's showing a successful JSON response with a correctly formed JSON object, with the 'Latitude' and 'Longitude' properties correctly set. I've tried organising the above code a different way so that the callback calls another function and passes the 'data' to it, instead of self-invoking. The separate function is never called, which seems to confirm that the .get() just isn't recognising a successful response. Why would that happen when I can see the successful response in the 'NET' tab of Firebug?

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206

3 Answers3

2

Add JSON as fourth parameter to the .get() function like:

$.get( "test.php", {}, function( data ) {
    console.log(data);// Check webconsole what you get in data        
}, "json" );
Subedi Kishor
  • 5,906
  • 5
  • 35
  • 53
  • Thanks Kishor, I still get the same result, although the following is now logged in the console: Object { RegionCode="G2", CountryId=29, CountryName="New Zealand", more...} - the Latitude and Longitude are there, which looks correct to me... – Chris Halcrow Nov 20 '13 at 05:04
  • Can you access lat and long now? – Subedi Kishor Nov 20 '13 at 05:22
  • Hi Kishor. Thanks, although no, unfortunately this didn't work. Upvote for the suggestion though :) – Chris Halcrow Nov 20 '13 at 20:53
1

I'll bet I know! Dump "data" to the console with "console.log(data)" as the first line within your callback function. Likely what you see there will have your returned data buried within a wrapper object. For example, you may have to look at data.data.Latitude or something similar to get down to the data which was sent from the function on the server.

John Munsch
  • 19,530
  • 8
  • 42
  • 72
  • Thanks John, I've tried this and the following is logged: Object { RegionCode="G2", CountryId=29, CountryName="New Zealand", more...} - the Latitude and Longitude are there, which looks correct to me... – Chris Halcrow Nov 20 '13 at 05:02
  • 1
    If it truly looked like that, it doesn't look correct to me. I've seen a similar problem with Java before where we weren't getting a real JSON object back to the client. In JSON it would have looked like { "RegionCode" : "G2", "CountryId" : 29, "CountryName" : "NewZealand"... } – John Munsch Nov 20 '13 at 05:06
  • Thanks John. I don't think it's malformed though, I think that Firebug on Firefox just formats the JSON like that in the console. Any other ideas? – Chris Halcrow Nov 20 '13 at 21:16
1

Have you tried using '$.getJSON()' instead?

$.getJSON(url, function (data) {
    g.currentLat = data.Latitude;
    g.currentLng = data.Longitude;
});

Have you tested if it fires an error? Try listening to '.fail();

It seems like '$.get()' should be capable of handling JSON automatically, maybe try telling it your receiving JSON. (Your response headers might be wrong)

$.get(url, {}, function (data) {
    g.currentLat = data.Latitude;
    g.currentLng = data.Longitude;
}, "json");
  • Sadly I can't comment on other answers yet, but I want to help... John Munsch: Are you sure it's malformed, or do you have any idea what format that is? I'm quite surprised it actually created an object formatted like that. Chris Halcrow: What browser are you debugging with? And how did your JSON look in Firebug? – GalaxyNetworks Nov 20 '13 at 05:23
  • Hi there. Thanks loads for the response. I'm using Firebug on Firefox, and when I output the JSON to the Firebug console, it's formatted like this - Object { RegionCode="G2", CountryId=29, CountryName="New Zealand", more...} - the Latitude and Longitude are there – Chris Halcrow Nov 20 '13 at 21:17
  • Brilliant thanks, now working using the code you suggested :) – Chris Halcrow Nov 20 '13 at 21:28