views:

270

answers:

2

I have this script that calls a .net WebService

msg = $.toJSON(
        $.ajax({
            type: "POST",
            url: "http://[url]/ETS.UI/WebServices/LocationService.asmx/GetMappingLocationDetails",
            contentType: "application/json; charset=utf-8",
            data: $.toJSON({'componentId':994}),
            dataType: "json",
            async: false
        }).responseText
);

And I recieve the following value in the msg variable:

""{\"d\":\"{\\\"ComponentId\\\":994,\\\"Latitude\\\":32.219627009236405,\\\"Longitude\\\":-110.96843719482422,\\\"LocationName\\\":\\\"Tucson\\\",\\\"StreetAddress\\\":\\\"7201 E 22nd Street \\\",\\\"City\\\":\\\"Tucson\\\",\\\"State\\\":\\\"AZ\\\",\\\"PostalCode\\\":null}\"}""

I have no idea why this would format this way, seems to only do this in responseText.

Does anyone have any ideas?

A: 
josh3736
Thanks Josh, I will try that.
BoredOfBinary
A: 

To answer your question directly - you're parsing the json twice - once because you specify dataType: 'json', and again with the $.toJSON. You can probably change the datatype to 'text' and it will solve your problem.

The correct answer though, is to use an asynchronous call with a callback. Someone else has answered the question as I've been typing, so look at their answer instead :)

--edit--

To answer your comments (because formatting doesn't really work in comments). You don't need to stringify the data - you can send it as an object. There's no need to declare the msg variable before the function. Async is the default, so you can just leave that out.

var list = [994, 994, 994, 994]; 
$.ajax({ 
  type: "POST", 
  url: "http://www.eaglerider.com/ETS.UI/WebServices/LocationService.asmx/GetMappingLocationDetails", 
  contentType: "application/json; charset=utf-8", 
  data: { componentIdCollection: list },
  dataType: "json", 
  sucess: function(msg){
    // msg holds your JSON data
    console.log(msg); 
  }
});

Once you've got that msg data, I'm guessing you want to append it to the list, then you would call the function that redraws the location data on the map.

zaius
sorry to string this along, I just don't seem to be getting this. So I turned async on, (which makes sense) and I write a function to the success attribute.
BoredOfBinary
var msg, list, jsonText; list = [994, 994, 994, 994]; jsonText = JSON.stringify({ componentIdCollection: list }); $.ajax({ type: "POST", url: "http://www.eaglerider.com/ETS.UI/WebServices/LocationService.asmx/GetMappingLocationDetails", contentType: "application/json; charset=utf-8", data: jsonText, async: true, dataType: "json", sucess: function(msg){ alert('yay'); }});
BoredOfBinary