0

オブジェクトをajaxでasp.netサーバーに送りたいです。オブジェクトには、認識されない日時プロパティが他にもあります。パラメータの日時がデフォルトであることを意味します=> 0001.01.01

  var a = {start: new Date(), title: "asdf", description: "asdfasdf", allDay: true, end: new Date()};

$.ajax({
            url : m_serverName + "/api/calendar",
            type : "post",
            data : a,
            dataType: 'json',
            success : function() {
                console.log("ok")
            },
            error : function(request, status, error) {
                console.log(request)
            },
            xhrFields : {
                withCredentials : true
            }
        });  

これがどのように機能するかという 1 つの方法しかありません。toJSON を使用し、それを JS としてサーバーに送信するとします。

サーバーコード:

 public HttpResponseMessage Post(CalendarEvent calendarEvent)
        {
            int id = -1;

            var httpCookie = HttpContext.Current.Request.Cookies["token"];
            if (httpCookie != null)
                if (!int.TryParse(httpCookie.Value, out id))
                    return Request.CreateResponse(HttpStatusCode.OK, false);
                else
                {
                    int employeeId = service.GetByEmployeeDevice(id).Id;
                    return Request.CreateResponse(HttpStatusCode.OK,
                                                  service.GetEventsById(employeeId));
                }

            return Request.CreateResponse(HttpStatusCode.OK, false);
        }

ドメインモデル

 [JsonObject]
    public class CalendarEvent
    {
        [JsonProperty("id")]
        public int Id { get; set; }

        [JsonProperty("employeeId")]
        public int EmployeeId { get; set; }

        [JsonProperty("title")]
        public string Title { get; set; }

        [JsonProperty("description")]
        public string Description { get; set; }

        [JsonProperty("location")]
        public string Location { get; set; }

        [JsonProperty("partner")]
        public string Partner { get; set; }

        [JsonProperty(PropertyName = "allDay")]
        public bool AllDay { get; set; }

        [JsonProperty(PropertyName = "start")]
        public DateTime Start { get; set; }

        [JsonProperty(PropertyName = "end")]
        public DateTime End { get; set; }

        [JsonProperty(PropertyName = "type")]
        public int Type { get; set; }

        [JsonIgnore]
        public Employee Employee { get; set; }

        //navigation property
        //public ICollection<AgentDevice> Devices { get; set; }
    }

これを変換せずにajaxで送信するにはどうすればよいですか。なぜなら、非常に多くのオブジェクトを持つ配列がある場合、それは非常に難しいからです。

4

3 に答える 3

2

ISO 8601それらを文字列として送信できます。

var a = {
    start: (new Date()).toISOString(), 
    title: "asdf", 
    description: "asdfasdf", 
    allDay: true, 
    end: (new Date()).toISOString()
};

サーバーでヒットしている ASP.NET Web API は、Newtonsoft Json シリアライザーを使用しており、DateTime インスタンスに適切に逆シリアル化できます。

于 2013-09-25T17:07:33.890 に答える
0

型付きリスト/配列を使用する List<CalendarEvent>CalendarEvent[]、データ型として使用するだけで、ISO-8601 でエンコードされた日付を適切にデコードする必要があります。

于 2013-09-25T17:29:41.157 に答える
0

秘密は次のとおりです。JsonConvert.DeserializeObject (customerJSON);

public HttpResponseMessage Post([FromBody]String customerJSON)
        {
            if (customerJSON != null)
            {
                int id = -1;

                var httpCookie = HttpContext.Current.Request.Cookies["token"];

                Customer customer = JsonConvert.DeserializeObject<Customer>(customerJSON);


                if (httpCookie != null)
                    if (!int.TryParse(httpCookie.Value, out id))
                        return Request.CreateResponse(HttpStatusCode.OK, false);
                    else
                    {
                        customer.ContactId = employeeService.GetByEmployeeDevice(id).Id;
                        return Request.CreateResponse(HttpStatusCode.OK,
                                                        service.Add(customer));
                    }
            }

            return Request.CreateResponse(HttpStatusCode.OK, false);
        }
于 2013-09-25T18:38:31.937 に答える