需要帮助使用jQuery AJAX将复杂对象传递给ServiceStack

时间:2013-08-06 01:00:40

标签: ajax json jquery servicestack ormlite-servicestack

我正在尝试使用对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的所有属性都为null,甚至是Activities对象。

非常感谢任何建议......

1 个答案:

答案 0 :(得分:1)

SerivceStack希望您send your complex type格式为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};
     });
}