locked
Calling a Web Service From HTML File With JQuery AJAX RRS feed

  • Question

  • User931778073 posted

    Hi all, I have a web service that has a simple web method that takes two parameters of type interger and it returns the result of adding the two numbers together. I made sure to uncomment the following line in my code behind:

    [System.Web.Script.Services.ScriptService()]

    I am trying to call this webservice from a regular html file using jquery ajax using the following code:

    $.ajax({
       type: "POST",
       url: "../MyService.asmx/AddNumbers",
       data: "{'a':" + $('#txtData1').val()+ 'b': + $('#txtData2').val()+ "}",
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: function (response, status){
       //alert(response.d);
       $('#output').empty();
       $('#output').append(response.d);},
       failure: function (){
       alert("Failed!");
       }
     });

    When I try to call the webservice from my html file I get the "Failed!" alert message. I don't know what else I have to include aside from what I aready have.

    Please help me resolve this problem, thanks in advance.

    Monday, February 17, 2014 10:15 AM

Answers

  • User931778073 posted

    Hello, thank you all for replying. I have modified my code as follows and I finally solved my problem:

    data: "{a:" + "'" + $('#txtData1').val() + "'" + "b:" + "'" + $('#txtData2').val() + "'" + "}",

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 19, 2014 12:16 AM

All replies

  • User-1205895928 posted

    Hi

    everything seems correct.

    Did you check putting a breakpoint inside webmethod, and check whether are getting the parameters properly?

    AND

    update the following input in the call.

    data: {"a": $('#txtData1').val(), "b":  $('#txtData2').val()},

    Thank you

    Monday, February 17, 2014 1:40 PM
  • Monday, February 17, 2014 10:48 PM
  • User-1768369891 posted

    hi

    try to use this.

    data: "{a:" + $('#txtData1').val()+ "b:" + $('#txtData2').val()+ "}",
    Monday, February 17, 2014 11:04 PM
  • User931778073 posted

    Hello, thank you all for replying. I have modified my code as follows and I finally solved my problem:

    data: "{a:" + "'" + $('#txtData1').val() + "'" + "b:" + "'" + $('#txtData2').val() + "'" + "}",

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 19, 2014 12:16 AM