0

I am trying to use web service using jquery ajax. The webservice method is called and the parameter are passed as well, I wonder why the ajax would not execute success function

Here's my ajax call

<script type="text/javascript">
    function btn_add() {
        var a = $("#tb_a").val();
        var b = $("#tb_b").val();
        var c = $("#tb_c").val();
        var para = "a=" + a + "&b=" + b + "&c=" + c;

        $.ajax({
            type: "POST",
            url: "mywebservice.asmx/add",
            data: para,
            dataType: "text",
            success: function (res) {
                alert("success");
                console.log(res);
            }
        });
    }
</script>

Edit

Here is my webservice code

 [System.Web.Script.Services.ScriptService]
    public class mywebservice : System.Web.Services.WebService
    {

        [WebMethod, ScriptMethod]
        public int add( int a, int b, int c)
        {
            return a + b + c;
        }
    }
suman
  • 728
  • 3
  • 10
  • 29
  • 1
    Check the console for errors. I would imagine from looking at your code it's a 400 or 500 error. If it's the latter, seeing your server side code would help too – Rory McCrossan Jun 10 '15 at 15:27

2 Answers2

0

You should take a look at the documentation, it says that your call could be like like this:

     $.ajax({
        type: "POST",
        url: "mywebservice.asmx/add",
        data: { a : $("#tb_a").val(), 
                b : $("#tb_b").val(),
                c : $("#tb_c").val() },
        success: function (res) {
            alert("success");
            console.log(res);
        }
    });

if in your server you have this:

public string myFunction(string a, string b, string c)
{
    return a + b + c;
}

now, if that doesn't work, take a look at this answer, it's a different way to accomplish what you want

Community
  • 1
  • 1
Enrique Zavaleta
  • 2,098
  • 3
  • 21
  • 29
0

You might be getting 404 error, check it your console, coz your add method signature is not matched what you send in your ajax call. You can try like this

var aData=[];
    aData[0] = $("#tb_a").val();
    aData[1] = $("#tb_b").val();
    aData[2] = $("#tb_c").val();
    var jsonData = JSON.stringify({ aData:aData});
     $.ajax({
            type: "POST",
            url: "mywebservice.asmx/add", 
            data: jsonData,
            contentType: "application/json; charset=utf-8",
            dataType: "json", // dataType is json format
            success: OnSuccess,
            error: OnErrorCall
        });
        
    function OnSuccess(response){
     console.log(response.d);
    }
    
    function OnErrorCall(response){
     console.log(response.d);
    }

 [WebMethod, ScriptMethod]
  public int add(list<string> aData)
 {
     int cal=Convert.ToInt32(aData[0])+Convert.ToInt32(aData[1])+Convert.ToInt32(aData[2]);
    return cal;
 }
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Satinder singh
  • 10,100
  • 16
  • 60
  • 102
  • It still won't work. If I apply breakpoint in ajax call in js, then it reaches the success function and shows result. If I don't apply breakpoint and just run the program, there is nothing. Even the console of firebug displays error for a second and disappear. I am so confused. – suman Jun 10 '15 at 23:56