发送JQuery的JSON到WCF REST使用日期 [英] Send JQuery JSON to WCF REST using date

查看:125
本文介绍了发送JQuery的JSON到WCF REST使用日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道很多有关通过JQuery的/ JSON消费WCF REST的职位,但我无法得到它的工作。我目前停留在一个日期参数。下面是我的C#方法:

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:

[OperationContract]
[WebInvoke]
[TransactionFlow(TransactionFlowOption.Allowed)]
string GoodRegister(DateTime pDtTimeStampTransac, Int32 pIDResource, Decimal pQty, enQtyLogType pQtyGoodLogType);



下面是我的JavaScript代码:

Below is my JavaScript code:

/// <reference path="../Scripts/jquery-1.4.1-vsdoc.js" />
/// <reference path="json.js" />

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

function botaoclick() {
  var date = new Date().toMSJSON();
  var datavar = {
    'pDtTimeStampTransac': date,
    'pIDResource': 1,
    'pQty': 1
  };
  $.ajax(
  {
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "http://desk01:9876/ShopFloorService/script/GoodRegister",
    dataType: "json",
    data: JSON.stringify(datavar),
    //data: '{"pDtTimeStampTransac":date, "pIDResource":"teste", "pQty":"3"}',
    error: jqueryError,
    success: function (msg) {
      alert("back");
      var divForResult = document.getElementById("test");
      divForResult.innerHTML = "Result: <b>" + msg.d + "</b>";
    }
  }
  )
}

function jqueryError(request, status, error) {
  alert(request.responseText + " " + status + " " + error);
}



我的第一个问题是,我不断收到日期序列化错误:

My first problem is that I keep getting a date serialization error:

{"ExceptionDetail":{"HelpLink":null,"InnerException":{"HelpLink":null,"InnerException":{"HelpLink":null,"InnerException":null,"Message":"DateTime content '\\\/Date(1292616078638)\\\/' does not start with '\\\/Date(' and end with ')\\\/' as required for JSON.","StackTrace":"   at System.Runtime.Serialization.Json.JsonReaderDelegator.ParseJsonDate(String originalDateTimeValue)\u000d\u000a   at 

它说,它不会开始/结束的方式它的开始和结束。

It says it doesn't start/end the way it's starting and ending.

我的第二个问题是:我一定要找枚举的顺风车,或者是有没有办法把它

My second question is: Will I have to get ride of the enumerator, or is there a way to send it?

推荐答案

我拿出了很多头发,在这个流下许多眼泪,但这个工作。我在 toMSJSON 函数修改日期格式。WCF接受这种格式,我意识到感谢里克施特拉尔

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

您还需要将日期转换为UTC时间,或者获得各种有趣的东西,所以:

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.

这篇关于发送JQuery的JSON到WCF REST使用日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆