Send JQuery JSON to WCF REST using date

前端 未结 6 507
慢半拍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:05

    I pulled out a lot of hair and shed plenty a tear over this but this worked. I modified the date formatting in your toMSJSON function. WCF accepts this format which I realised thanks to Rick Strahl.

    Date.prototype.toMSJSON = function () {
        var date = '/Date(' + this.getTime() + ')/'; //CHANGED LINE
        return date;
    };
    

    You also need to convert the dates to UTC time or you get all kinds of funny stuff, so:

      var dt = ...;
      var dt1 = new Date(Date.UTC(dt.getFullYear(), dt.getMonth(), dt.getDate(),   dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMilliseconds()));
      var wcfDateStr = dt1.toMSJSON();
    

    Hope this helps.

提交回复
热议问题