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

查看:28
本文介绍了使用日期将 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)u000du000a   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 接受这种格式,我意识到这要归功于 Rick Strahl.

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

希望这会有所帮助.

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

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