1

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.

4
  • Does the call actually returns data ? You might maybe specify the contentType: "application/json; charset=utf-8"and dataType: "json" options. (source) Commented Nov 16, 2011 at 19:55
  • Yes, as I mentioned, all the data appears to be there (looking with the debugger) and Fiddler shows it is being returned. I had tried both those settings (and many others) but it didn't help. Commented Nov 16, 2011 at 19:57
  • The D is normal in the answer. It's a security that Microsoft add to Json. Commented Nov 16, 2011 at 20:19
  • @Daok: I knew that the "D" was a security measure from MS. But it seems like the request is working fine, but that I'm getting a parse error on the returned data somewhere. So it seems natural to wonder if something is having trouble parsing the "D". Commented Nov 16, 2011 at 22:24

3 Answers 3

2

Try adding:

contentType: "application/json; charset=utf-8" ,
dataType: "json",
type: "POST",

To your ajax request

On your success handler, call:

alert('Success: ' + results.d.length);

It should alert a 5

Also, I haven't seen that "failure" handler before. I would comment it out for now just in case... I think the "error" handler should suffice.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply. In trying to resolve the issue, I had already tried those additional property values. But just for good measure, I pasted your code into mine but got exactly the same result. Can't do the alert() statement because I don't get that far. Also removed the failure handler. Same results.
1

Here is what I use to call my asmx service. Your other code looks good.

 <script type="text/javascript">  
   function testAsmx() {  
    $.ajax({  
       type: "POST",  
       url: "http://localhost/ASMXtoWCF/test.asmx/HelloWorld",  
       dataType: "json",  
       data: '{"variable":"testme"}',  
        contentType: "application/json; charset=utf-8",  
        success: function (msg) {  
          alert(msg.d);  
        },  
        error: function (msg) {  
          alert("I am a failure");  
        }  
      });  
    }  
</script>

Comments

1

RESOLVED

I was finally able to correct this issue. It seems it was a combination of the fact that we were using an old version of jQuery (1.2.6), and the way we were configuring it. Specifically, we had the line dataType: "json" in $.ajaxSetup().

For reasons I still don't fully understand, removing this line caused the code to start working as expected.

Thanks for all the comments. Guess it would've been hard to resolve this issue with the information I provided.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.