首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

请问个JSON异步获取的有关问题

2013-03-25 
请教个JSON异步获取的问题后台代码[HttpPost]public JsonResult GetShipId(){var ship db.chartered_shi

请教个JSON异步获取的问题
后台代码

[HttpPost]
        public JsonResult GetShipId()
        {
            var ship = db.chartered_ship_base_info.ToList();
            StringBuilder sb = new StringBuilder();
            sb.Append("<option value="-1">select....</option>");
            foreach (var item in ship)
            {
                sb.AppendFormat("<option value="{0}">{1}</option>", item.chartered_ship_number, item.chartered_ship_number);
            }
            string result = sb.ToString();
            return this.Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult GetCabinId(string shipnumber)
        {
            
            var cabin = db.ship_cabin_info.Where(c => c.chartered_ship_number == shipnumber).ToList();
            StringBuilder sb = new StringBuilder();
            sb.Append("<option value="-1">select.....</option>");
            foreach (var item in cabin)
            {
                sb.AppendFormat("<option value="{0}">{1}</option>", item.cabin_number, item.cabin_number);
            }
            string result = sb.ToString();
            return this.Json(result, JsonRequestBehavior.AllowGet);
        }




前台代码
<script type="text/javascript">

    $(function () {
        $.ajax({
            url: "/TestJosn/GetShipId",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: "{}",
            dataType: "json",
            success: function (data) {
                $("#ShipId").empty();


                $("#ShipId").html(data);
            },
            error: function ErrorCallback(XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown + ":" + textStatus);
            }
        });
        // Get the departments depend on the school
        $("#ShipId").change(function () {
            GetCabinIdFromId($("#ShipId").val());
        });
    });

    function GetCabinIdFromId(sId) {
        $.ajax({
            url: "/TestJosn/GetCabinId",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: "{ShipId:" + sId + "}",
            dataType: "json",
            success: function (data) {
                $('#CabinId').empty();
                $('#CabinId').html(data);
            },
            error: function ErrorCallback(XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown + ":" + textStatus);
            }
        });
    }


</script>
<h2>
    Index</h2>
<select id="ShipId">
    <option value="-1">select....</option>
</select>
<select id="CabinId">
    <option value="-1">select....</option>
</select>


问题是,GetCabinId(string shipnumber)这里总是获取不到shipnumber 的值,shipnunber总是null 是为什么呢?应该怎么改?谢谢

[解决办法]
http://stackoverflow.com/questions/13625785/asp-net-webapi-throw-404-if-method-parameter-is-string-or-int/13628252#13628252
[解决办法]
data: {"ShipId": sId},
data这里这样写

热点排行