0

I want to send an object by ajax to the asp.net server. There is more datetime property in the object what are not recognised. I mean the datetime in the parameter is the default => 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
            }
        });  

there is just one way, how this works. If I use the toJSON and I send it to the server as JS.

server code:

 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);
        }

domain model

 [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; }
    }

How can I send this by ajax, without any convertation, because if I have an array with somany object it is very difficult.

3 Answers 3

2

You could send them as ISO 8601 strings:

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

The ASP.NET Web API you are hitting on the server uses Newtonsoft Json serializer which will be able to properly deserialize them to DateTime instances.

Sign up to request clarification or add additional context in comments.

1 Comment

I could use the toJSON method, and the server will recognize it, but if I have an array with more thousands item. Do I have to convert all date property of all of the array items?
0

Just use a typed list/array... List<CalendarEvent> or CalendarEvent[] as your datatype, and it should decode the ISO-8601 encoded dates appropriately.

2 Comments

I understand, but before I have to convert the date object toJSON, or to ISOString, do not I?
In JavaScript, when you serialize something with JSON.stringify(...) the default is to serialize date-time instances using UTC/GMT as ISO-8601 style datetime.
0

the secret is: 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);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.