I'm trying to call a webservice from an ASP.NET web page using AJAX. But it always calls my error handler and not my success handler.
Here's my javascript:
function DeleteCurrency(currenciesId) {
$.ajax({
url: "Ajax/Currencies.asmx/GetCurrencyUsage",
data: "{ 'currencyId' : '" + currenciesId + "' }",
failure: function (msg) {
alert('Failure: ' + msg);
},
error: function (result, thrownError) {
alert('Error:');
},
success: function (results) {
alert('Success: ' + results);
}
});
}
Here's the code behind in my asmx file:
/// <summary>
/// Summary description for Currencies
/// </summary>
[WebService(Namespace = "http://xxx.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
[System.ComponentModel.ToolboxItem(false)]
public class Currencies : System.Web.Services.WebService
{
[WebMethod]
public string[] GetCurrencyUsage(int currencyId) {
List<string> list = new List<string>();
list.Add(currencyId.ToString());
list.Add("This is item 1");
list.Add("This is item 2");
list.Add("This is item 3");
list.Add("This is item 4");
list.Add("This is item 5");
return list.ToArray();
}
}
According to Fiddler, here's what I'm sending:
{ 'currencyId' : 3 }
And here's what the webservice returns:
{"d":["3","This is item 1","This is item 2","This is item 3","This is item 4","This is item 5"]}
As mentioned, my error handler gets called. But the status of the result argument shows "OK" and 200. The second argument is of type parsererror.
All the data appears to be there, so where is the error. Does it have something to do with the "d": in the results? I'm not sure where to look next.
EDIT:
Further testing reveals that if I change my web service to return a single string (rather than an array), everything works as expected.
Clearly, my web service is being called correctly, it is returning a status of 200 (OK), and it is also returning the expected data. But there is some kind of error parsing the results when there are multiple values. I'm still wondering if it has something to do with the "D", but I'm just not sure.
contentType: "application/json; charset=utf-8"anddataType: "json"options. (source)