locked
Displaying A String Returned From A Web Service In An AJAX Call RRS feed

  • Question

  • User1231829591 posted

    Hi all,

    My web method in my web service successfully retrieved the total row count from a database. If the return value is zero the variable "is_unique" is set to "true" else if the return value is 1 or more, it is set to "false". Then the variable "is_unique" is returned. The following are my code snippets.

    //--------------- Web Service Code Snippet

    [WebMethod]
    public string function GetTotal(string firstname, string lastname)
    {

    if(firstname.length==0){return"Please enter first name";};

     if(lastname.length==0){return"Please enter last name";}; 

    string is_unique = "false";

    //---- ADO.NET Code goes here
    //---- More ADO.Net code
    //---- More ADO.NET code
    //---- More ADO.NET code
    //---- More ADO.NET code

    con.Open();
    cmd.ExecuteNonQuery();

    int count = Convert.ToInt32(outputParam.Value);
    if (count == 0) { is_unique = "true"; }
    else if(count>=1) { is_unique = "false"; }
    con.Close();
    return is_unique;
    }
    //--------------AJAX function on the frontend 
    
    function IsUnique() {
    var jdata = {firstname: "Test", lastname: "Test"};
                $.ajax({
                    url: "http://localhost:21577/MyService.asmx/GetTotal",              
                    data:jdata,
                    method: "Post",                
                    dataType: "xml",
                    success: function (data) {                 
                        alert(data.value);
                    }
                });
    }

    The problem I am having is that when I try to retrieve data from the web service in the AJAX call on the frontend using "data.value", I get "Undefined". Please help me resolve this problem.

    Thursday, September 24, 2015 11:21 PM

Answers

  • User1231829591 posted

    Hi, thanks for replying but what you said has nothing to do with my problem since I'm using the XML format. I already figured out where I went wrong. It had to do with passing the xml element from WSDL that holds my string, to the "find( )" method in the success portion of my AJAX function.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 27, 2015 9:57 AM

All replies

  • User1724605321 posted

    Hi ManyTitles ,

    You could use json as the dataType ,and get the return string as follows:

      $.ajax({
                    url: "http://localhost:21577/MyService.asmx/GetTotal",              
                    data:jdata,
                    method: "Post",                
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {      
                        alert(data.d);
                    }
                });
    

    For more details ,please refer to links below:

    http://api.jquery.com/jquery.ajax/ .

    http://www.c-sharpcorner.com/UploadFile/dacca2/understand-jquery-ajax-function-call-web-service-using-jque/ .

    Best Regards,

    Nan Yu

    Friday, September 25, 2015 3:21 AM
  • User1231829591 posted

    Hi, thanks for replying but what you said has nothing to do with my problem since I'm using the XML format. I already figured out where I went wrong. It had to do with passing the xml element from WSDL that holds my string, to the "find( )" method in the success portion of my AJAX function.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 27, 2015 9:57 AM