I have the following ajax function using jsonp:
function PopulateDivisions1()
{
$.support.cors=true;
$.ajax({
type:'GET',
url:'http://IP/Service/api/DivisionSearch/GetAllDivisions?callback=?',
dataType: "jsonp",
//jsonp: false,
jsonpCallback: "myJsonMethod",
success: function(data) {
alert('yes');
$("#divisionSelect").append($('<option></option>').val("-99").html("Select One"));
$.each(data, function(i, item){
$("#divisionSelect").append($('<option></option>').val(item.Name).html(item.Name));
});
},
error: function(xhrequest, ErrorText, thrownError) {
alert("Original: " + thrownError + " : " + ErrorText);
}
});
}
I am getting the following error: myJsonMethod was not called : parsererror
If I look at Fiddler, I am getting the following data back, I added the callback name to the front, as I saw that suggested, if I take it out it still doesn't work.
"myJsonMethod([{\"Id\":1,\"Description\":\"Executive\",\"Name\":\"Executive \"},{\"Id\":2,\"Description\":\"ASD\",\"Name\":\"Administrative Services Division \"},{\"Id\":3,\"Description\":\"COM\",\"Name\":\"Communications \"},{\"Id\":4,\"Description\":\"CP\",\"Name\":\"Contracts and Procurement \"},{\"Id\":5,\"Description\":\"PMD\",\"Name\":\"Program Management Division \"},{\"Id\":6,\"Description\":\"RED\",\"Name\":\"Research and Evaluation Division \"},{\"Id\":7,\"Description\":\"IT\",\"Name\":\"Information Technology \"}])"
Here is the method in my controller:
public string GetAllDivisions(string callback)
{
var divisions = _DivisionModel.GetAllDivisions();
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = serializer.Serialize(divisions);
string result = callback + "(" + json + ");";
return result;
}
I'm not getting to my success call, what am I missing or doing wrong?