Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Following is my ajax function:

JavaScript
$("[id*=DropDownListSubBrand]").change(function () {
                var subBrandval = $('[id*=DropDownListSubBrand]').val();
                alert(subBrandval);
                //create the ajax request
                $.ajax({
                    type: "POST", //HTTP method
                    url: "PCAdd.aspx/OnSubBrandChange", //page/method name
                    data: "{'Brand_Id':'" + $('[id*=DropDownListSubBrand]').val() + "'}", //json to represent argument
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) { //handle the callback to handle response                
                        //request was successful. so Retrieve the values in the response.                        
                        alert(msg.valueOf());

                    }                 

                });
            });


Following is the function that is being called from inside the above function:

C#
[System.Web.Services.WebMethod]
        public static string OnSubBrandChange(int Brand_Id)
        {
            Brand objBrand = new Brand();
            string strBrand=objBrand.GetBrand(Brand_Id);

            return strBrand;
        }


The response that I receving from the "OnSubBrandChange" is correct when I am seeing it with a breakpoint on the "return" statement in the above function but the "alert" startement inside "success(msg)" is returning [object Object]. How would i get a proper string from the ajax method?
Posted
Comments
stibee 23-Feb-15 4:45am    
Debug this and control which value does "msg" have on client

If you want to get the underlying JSON object as string, try JSON.stringify(msg);
 
Share this answer
 
C#
$("[id*=DropDownListSubBrand]").change(function () {
                var subBrandval = $('[id*=DropDownListSubBrand]').val();
                alert(subBrandval);
         $.ajax({
                type: "POST",
                url: "PrintEmployee.aspx/OnSubBrandChange",
                data: "{'Brand_Id':'" + $('[id*=DropDownListSubBrand]').val() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                beforeSend: function() { $("#wait").show(); },
                complete: function() { $("#wait").hide(); },
                success: function(data) { alert(data.d); },
                error: function() { alert('Error Occured'); }
            });


and in C# Code just write
C#
[System.Web.Services.WebMethod]
public static string OnSubBrandChange(int Brand_Id)
{
      Brand objBrand = new Brand();
      string strBrand=objBrand.GetBrand(Brand_Id);
 
      return strBrand;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900