1

当我将表单数据发布到我的网络服务时,我遇到了问题。当我调试 Web 服务时,参数 messagepost 的计数为 1,但对象数据为空,但是当我查看请求中的对象时,我可以看到该对象已填充。我一直在寻找和绞尽脑汁一个小时,但看不到我让它工作。所以也许这里有人可以帮助我。

Javascript

function getEmptyLeg() {

        var chkTail = $("#elTail").is(':checked');
        var chkType = $("#elType").is(':checked');
        var chkNBAA = $("#chkNBAA").is(':checked');
        var chkCategory = $("#elCategory").is(':checked');

        var itineraryList = new Array();

        var itinerary = new Object();
        itinerary.Time = "";
        itinerary.Arrival = $("#elTo").val();
        itinerary.Departure = $("#elFrom").val();
        itinerary.ArrivalDate = getDate("#elEndDate");
        itinerary.DepartureDate = getDate("#elStartDate");
        itineraryList.push(itinerary);

        var messagepost = new Object();
        messagepost.Types = "";
        messagepost.Categories = "";
        messagepost.ShowTail = chkTail;
        messagepost.ShowType = chkType;
        messagepost.SendToNBAA = chkNBAA;
        messagepost.DirectoryAircraft = false;
        messagepost.ShowCategory = chkCategory;
        messagepost.Price = $("#elPrice").val();
        messagepost.NBAAEmail = $("#txtEmail").val();
        messagepost.MessageItinerary = itineraryList;
        messagepost.AircraftID = $("select#elAircraft").val();                       

        return messagepost;
    }

function saveMessage() {

        var options = "";
        var type = $("#hdfType").val();
        var userid = $('#<%= hdfUserID.ClientID %>').val();
        var message = $("#<%= txtMessage.ClientID %>").val();
        var timezone = $('#<%= hdfTimezone.ClientID %>').val();

        $('#<%= sltUsers.ClientID %> option:selected').each(function (i) {
            options += $(this).val() + ",";
        });

        var messagepost = new Array();
        messagepost.push(getEmptyLeg());


        $.ajax({
            type: "Post",
            async: false,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            url: "/Resources/MessageWebService.asmx/SaveMessage",
            data: "{ 'message':'" + message + "',"
                    + "'options':'" + options + "',"
                    + "'url':'" + $(location).attr('href') + "',"
                    + "'userid':'" + userid + "',"
                    + "'messageid':'" + 0 + "',"
                    + "messagepost:" + JSON.stringify(messagepost) + ","
                    + "'postType':'" + type + "' }",
            success: function (data) {                            

                    $('#messages').prepend($(data.d).fadeIn('slow'));

                    growlMessage('Message was successfully');
                },
            error: function (data) {
                showError(data.responseText);
            }
        });
    }

网络服务

[WebMethod]
public string SaveMessage(string message, string options, string url, string userid, string messageid, List<MessagePost> messagepost, string postType) {
}

来自 chrome 的请求

{ 'message':'empty leg','options':'Public,','url':'http://localhost/portal/quote.aspx?qid=254','userid':'d2703dd0-c743-4995-ac93-5cfab7fa5205','messageid':'0',messagepost:[{"Types":"","Categories":"","ShowTail":true,"ShowType":true,"SendToNBAA":true,"DirectoryAircraft":false,"ShowCategory":true,"Price":"2500","NBAAEmail":"chad.neiro@avianis.com","MessageItinerary":[{"Time":"","Arrival":"KSAN","Departure":"ENNA","ArrivalDate":"12/21/2012","DepartureDate":"12/21/2012"}]}],'postType':'4' }

留言帖类

public class MessagePost
{        
    Boolean ShowTail { get; set; }
    Boolean ShowType { get; set; }
    Boolean SendToNBAA { get; set; }
    Boolean ShowCategory { get; set; }
    Boolean DirectoryAircraft { get; set; }

    string Types { get; set; }
    string Price { get; set; }
    string NBAAEmail { get; set; }        
    string AircraftID { get; set; }
    string Categories { get; set; }

    List<MessageItinerary> ItineraryList { get; set; }
}

public class MessageItinerary
{
    string Time { get; set; }
    string Arrival { get; set; }
    string Departure { get; set; }
    string ArrivalDate { get; set; }
    string DepartureDate { get; set; }
    string Preference { get; set; }
}
4

1 回答 1

2

就像 saarps 在评论中建议的那样,我也相信这与 JSON 和数据的反序列化有关。

我很早就放弃了 WebMethods 中的参数,而是从 Request 中检索数据。例如,以下代码:

[WebMethod]
public string SaveMessage(string message) {
    //Do something
}

而是这样写:

[WebMethod]
public string SaveMessage() {
    string message = HttpContext.Current.Request.Form["message"];
    //Do something
}
于 2012-12-21T08:25:48.633 回答