1

This is my simple code to call asmx webservice (xml).

function maxTransaccion() {
    $.ajax({
        type: "POST",
        url: "WebService.asmx/MAxTransaccion",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        crossDomain: true,
        success: function(s) {
            return s.d;
        }
    });
}

But I received this error:

message: "s is not defined" proto: Error

I am doing something wrong? I use this ajax structure multiple times within a .js file. But only in this function it gives me error, what scares me is that it is so simple

Jamiec
  • 133,658
  • 13
  • 134
  • 193
Ljgazzano
  • 91
  • 1
  • 7
  • 3
    not clear how s can not be defined in that code, but returning from an asynchronous call makes no sense. – epascarello Aug 09 '19 at 12:52
  • Add a second argument to your success function for the status text, you may be getting an unexpected response. Or see network tab in your developer tools. – John Pavek Aug 09 '19 at 12:57
  • You said `contentType: "application/json; charset=utf-8",` but there is no `data` property so you can't be posting JSON. That doesn't make sense. – Quentin Aug 09 '19 at 13:11
  • Look at the Network tab in your browser's developer tools. Look at the response. That should show you that it is undefined … then look at the web service to work out why it is returning that – Quentin Aug 09 '19 at 13:12
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Liam Aug 09 '19 at 15:31

1 Answers1

0

First of all, if your service responds with a XML, then you should adapt for that:

    $.ajax({
        type: "POST",
        url: "WebService.asmx/MAxTransaccion",
        dataType: "xml",
        crossDomain: true,
        success: function(s) {
            return s.d;
        }
    });

I think changing dataType and omitting contentType might do the trick.

The next thing that could be improved is your success-handler.

Check for the property first, before using it:

        function(s) {
            if (s && s['d']) {
              doSomethingWith(s.d);
            }
        }

But because you are most likely receiving a XML and not a JSON-object, you might want something like this:

  function(xml) {
    var responseNode = $(xml).find('node');
    doSomethingWith(responseNode.text());
  }

Also like mentioned in the comments, just returning in an AJAX-call, will probably do nothing. So you need another function, where you get your result and doSomethingWithIt.

Sebastian G. Marinescu
  • 2,374
  • 1
  • 22
  • 33