为什么我的JCF值不出现在我的WCF操作中?

时间:2011-09-15 13:50:09

标签: c# wcf

我在从JQuery代码获取WCF操作的值时遇到问题。我有一个WCF操作,声明如下:

[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public ResultList<MyResult> FindResults(string latitude, string longitude)
{
  // latitude and longitude are always "0" here.
}

我的JQuery代码如下所示:

var latitude = GetLatitude();
var longitude = GetLongitude();
alert(latitude + ", " + longitude);

var json = { "latitude": latitude, "longitude": longitude };
$.ajax({
  url: "/services/myService.svc/FindResults",
  contentType: "application/json; charset=utf-8",
  data: json2string(json),
  dataType: "json",
  success: findResultsCompleted,
});

当我查看Fiddler时,我按预期收到200状态代码。请求标题显示:

GET /services/myService.svc/FindResults?{"latitude":33.041599,"longitude":-119.298798}

我做错了什么?为什么操作码中的纬度和经度始终为0?谢谢!

2 个答案:

答案 0 :(得分:0)

如果它们是字符串,为什么不是引号中的json?

"latitude":33.041599,"longitude":-119.298798

而不是

"latitude":"33.041599","longitude":"-119.298798"

您是否可以更改json以将值括在引号中或更改操作以接受double s?

答案 1 :(得分:0)

GET次请求需要?Param=Value

等参数

您可以将OperationContract切换为WebInvoke,将您的JS服务呼叫切换为POST

或者如果你想坚持GET,你可以删除stringify调用,jQuery应该正确地设置GET参数。