简体   繁体   English

AJAX响应文本始终为空

[英]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 我正在使用asp.net Web表单,并尝试使用AJAX从Java脚本函数调用Web服务方法,该函数返回带有车号和车号ID对的JSON数组,Web方法驻留在文件后面的同一代码中我在Java脚本函数页面上使用调试器多次检查了json变量,并确保它包含有效的JSON数组,但是当我在FillLimoCars Java脚本函数中检查cars对象时,我发现响应文本为空数组我不确定为什么会发生这种情况,我希望你们中的任何人都能提供帮助

Java Script Java脚本

            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# 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. 要成功,您必须取消序列化JSON对象。

$.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);
                          }
                  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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