locked
Ajax call to web service does not work if Parameters are passed RRS feed

  • Question

  • User1362505679 posted

    Hi,

    I have a web user control, i am trying to make an Ajax call to web service on hyperlink click event. 

    When I am trying to send parameters from AJAX call to web method, I always end up in getting the message : Parameter <name> is missing. Below is my ajax call.

    $('a[href="#next"]').click(function () {

    var obj = {};

    obj.City = "Test City";
    obj.State = "Test State";

    $.ajax({
    url: '<%=ResolveUrl("~/WebServices/Level1ExamSaveData.asmx/HelloWorld")%>',
    data: JSON.stringify(obj),
    type: "POST",
    datatype: "JSON",
    contenttype: "application/json; charset=utf-8",
    async: true,
    success: function (r) {
    alert(r);

    if (returnval == "City") {
    alert("server side method called successfully");
    }
    },
    error: function (xhr, ajaxoptions, thrownerror) {
    alert('exception:'+thrownerror + "value: "+xhr.responseText);
    }
    }).done(function () {
    $('#<%=txtcity.clientid%>').val('test modifed');
    });

    });

    And Web method is this: 

    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function HelloWorld(ByVal City As String, ByVal State As String) As String
    Return "City"
    End Function

    I have observed, the method gets called successfully if the web method doesn't have parameters (like below).

    <WebMethod()> _

    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _

    Public Function HelloWorld() As String
    Return "Hello World"
    End Function

    I have checked the response on success, its a [object XMLElement], so I am assuming the AJAX call is made using XML not JSON. Please let me know if my assumption is right, and help me with a solution. 

    Regards,

    Siva Karthik

    Friday, September 18, 2015 10:30 AM

Answers

  • User1362505679 posted

    We figured it out. The problem was with the java script code that I have written, most of them are problem with case-sensitivity.

    The ajax call changed to this, it works like charm.

    $('a[href="#next"]').click(function() {
    $.ajax({
    type: "POST",
    url: '<%=ResolveUrl("~/WebServices/Level1ExamSaveData.asmx/HelloWorld")%>',
    data: "{City: 'Toronto'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    //async: true,
    success: function(msg) {
    alert("This is successful");
    },
    error: function (xhr, ajaxoptions, thrownerror) {
    alert('exception:'+thrownerror + "value: "+xhr.responseText);
    }
    }).done(function () {
    $('#<%=txtcity.clientid%>').val('test modifed');
    });

    Thanks,

    Siva Karthik

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 18, 2015 12:54 PM