简体   繁体   中英

AJAX Response Text Is Always Empty

I'm using asp.net web forms and trying to call a web service method from a java script function using AJAX which returns a JSON array with a car number and a car number ID pairs the web methods resides in the same code behind file of the java script function page I checked the json variable more than one time using the debugger and made sure it holds a valid JSON array however when I checked the cars object in the FillLimoCars java script function I found the response text to be an empty array I'm not sure why this is happening I hope that anyone of you would be able to help

Java Script

            function testButton(carModelID) {
            $.ajax({
                type: "POST",
                url: baseURL + "WebPages/NewPages/ListCarRental.aspx/FillLimoCars",
                data: "{CarModelID:'" + carModelID + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                complete: FillLimoCar
                });
        }

        function FillLimoCar(cars)
        {
            var rdd_LimoCars = $find("<%=rdd_LimoCarNumber.ClientID %>");
            var comboItem = new Telerik.Web.UI.RadComboBoxItem();
            for(var i = 0; i < cars.length; i++)
            {
                ;
            }
        }

C#

    [WebMethod]
public static string FillLimoCars(int CarModelID)
{
    try
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        List<LimoFleet> limos = carsBLL.GetCarModelLimoFleet(CarModelID);
        List<object> objLimosList = new List<object>();
        foreach(var limo in limos)
        {
            var limoObj = new
            {
                CarID = limo.CarID,
                CarNumber = limo.CarNumber
            };
            objLimosList.Add(limoObj);
        }
        var json = serializer.Serialize(objLimosList);
        return json;
        //Context.Response.Write(objLimos.ToJson());
    }
    catch (Exception ex)
    {
        Tracer.Singleton.Log(ex);
        return null;
    }
}

You have to use success attribute. Inside success, you have to de serialize JSON object.

$.ajax({
                type: "POST",
                url: baseURL +     "WebPages/NewPages/ListCarRental.aspx/FillLimoCars",
                data: "{CarModelID:'" + carModelID + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                "success": function (json) {
                      if (json.d != null) {
                              FillLimoCars(json.d);
                          }
                  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM