I have this function to get me back a list of managers
function getManagers() {
var jqxhr = $.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: '/webservice.asmx/GetManagers',
dataType: 'json'
}).success(function(data) {
var options = '<option selected="selected" disabled="disabled">Select Manager</option>';
for (var i = 0; i < data.length; i++) {
options += '<option value="' + data[i].PostRef + '">' + data[i].Description + '</option>';
}
$('#ReceivingCellManager').html(options);
}).error(function(data) {
$('.ErrorText').html('Manager load failed please refresh page with F5');
$("#errormessage").dialog('open');
}).complete(function() {
});
}
as you can see I am using JQuery and want to popoulate a drop down list with the available managers
the method within my service looks like this
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetManagers()
{
using (var context = new ConcessionModel())
{
var rcm = Business.GetManager();
var serializer = new JavaScriptSerializer();
var response = rcm.Count() != 0
? serializer.Serialize(rcm)
: serializer.Serialize(new Error { Code = "500", Message = "Manager Retrieval Failed" });
this.Context.Response.Clear();
this.Context.Response.ContentType = "application/json";
this.Context.Response.Write(response);
}
}
When the method is called I recieve a response of 200 OK and the response contains the JSON I want the problem I am having is that the response is not being recognised as JSON.
I HAVE TRIED
- adding dataType to the ajax call as you can see above
- removing this.Context.Response.flush from the end of the response, this cured an error I was getting with adjusting headers after they were sent.
- adding a response format to the method
- adding a Response.ContentType to the Context These have all failed to get me the required recognition of the JSON. Any help would be much appreciated.
UPDATE: JSON FORMAT
{"Description":"data","Code":"data","reference":"data"}
UPDATE JSON RESPONSE I am seeing something weird in the response my response is as follows
[{"Description":"data","Code":"data","reference":"data"}]{"d":null}
I am not sure what the d null object is