2

我正在尝试使用对 GET 请求的 jQuery AJAX 调用将复杂对象传递给 ServiceStack。

代码如下所示:

模型:

public class Filter
{
    #region constructors

    public Filter()
    {
    }

    #endregion

    #region properties

    [AutoIncrement]
    [PrimaryKey]
    public int ID { get; set; }
    public bool? Huddles { get; set; }
    public List<Activity> Activities { get; set; }
    public string[] Looker { get; set; }
    public string LookerGender { get; set; }
    public string[] Lookee { get; set; }
    public string LookeeGender { get; set; }
    public DateTime? MinDate { get; set; }
    public DateTime? MaxDate { get; set; }
    public bool? ASAP { get; set; }
    [References(typeof(ZipCode))]
    public int? ZipCode { get; set; }
    public int? Radius { get; set; }

    #endregion
}

服务:

[Route("/v1/upfors/search")]
public class SearchUpFors 
{
    public Filter Filter { get; set; }
}

public class UpForService : Service
{
    public List<UpForView> Get(SearchUpFors request)
    {
        OrmLiteConfig.DialectProvider = MySqlDialect.Provider;
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ufsbe"].ConnectionString;
        using (IDbConnection db = connectionString.OpenDbConnection())
        {
            //TO DO
            //Build where clause based on filter properties
            List<UpForView> allUpFors;
            //If Activity(ies) selected, get filtered list
            if (request.Filter.Activities.Count > 0)
            {
                string[] activityNames = new string[request.Filter.Activities.Count];
                int i=0;
                foreach (Activity activity in request.Filter.Activities)
                {
                    activityNames[i] = activity.Name;
                    i++;
                }
                allUpFors = db.Select<UpForView>(q => Sql.In(q.Activity, activityNames));
            }
            else
                allUpFors = db.Select<UpForView>();
            return allUpFors;
        }
    }

}

客户端代码:这会填充复杂对象并将其作为 JSON 传递给 ServiceStack 服务。

    $('#frmFilter').submit(function (e) {
        e.preventDefault();
        var data = {};
        var activities = [];
        $('#ddlActivity :selected').each(function (i, selected) {
            activities.push({ ID: $(selected).val(), Name: $(selected).text() });
        });
        data["Activities"] = activities;
        data["ZipCode"] = $('#txtZipCode').text();
        data["Radius"] = $('#txtRadius').text();
        $.ajax({
            url: "service url here",
            type: "GET",
            data: "{'Filter':" + JSON.stringify(data) + "}",
            contentType: "application/json",
            dataType: "xml"
        });
    });

问题:

该对象在服务中创建,但 request.Filter 的所有属性都为空,甚至是活动对象。

任何建议将不胜感激...

4

1 回答 1

1

SerivceStack 期望您发送以JSV格式而不是JSON格式的复杂类型

您可以使用 ServiceStack 项目中的 JSV.js 之类的东西以 JSV 而不是 JSON 格式请求,或者实现您自己的Request Binder来反序列化 JSON。

public override void Configure(Container container)
{
    RequestBinders.Add(typeof (SearchUpFors), (IHttpRequest httpReq) => {
        var filter = JsonSerializer.DeserializeFromString<Filter>(httpReq.QueryString.Get("Filter"));
        return new SearchUpFors {Filter = filter};
     });
}
于 2013-09-20T21:58:15.650 回答