Send JQuery JSON to WCF REST using date

前端 未结 6 506
慢半拍i
慢半拍i 2020-12-08 08:54

I know that are a lot of posts about consuming a WCF REST through JQuery/JSON, but I can\'t get it to work. I\'m currently stuck at a date parameter. Below is my C# method:<

6条回答
  •  粉色の甜心
    2020-12-08 09:18

    Building over the answer by @vas above:-

    // OrderRecievedDateTime is a proper date string
    var tStart = new Date(OrderRecievedDateTime);
    // Get date in UTC (required for WCF) as morning
    var start = new Date(Date.UTC(tStart.getFullYear(), tStart.getMonth(), tStart.getDate(), 0, 0, 0));
    // Get the ticks
    var startTicks = start.getTime();
    
    // Now build the JSON param (**notice I am passing the date value as a string, by including within quotes. Without this it doesn't takes it**).
    var paramRequest = '{ "request": { "StartDate":"' + '\/Date(' + startTicks  + ')\/"'  + ' } }';
    
    // Hit ajax, no need of any JSON.parse or stringify
    $.ajax({ ..., data = paramRequest ..});
    

    WCF receives the date in proper format. Important addition is how we passed the date in JSON as a string. Can be further simplified as follows by combining the key and start of /Date string

    var paramRequest = '{ "request": { "StartDate":"\/Date(' + startTicks  + ')\/" } }';
    

提交回复
热议问题