简体   繁体   中英

Why aren't my JSON values appearing in my WCF operation?

I am having problems getting values to my WCF operation from my JQuery code. I have a WCF operation with the following declaration:

[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.
}

My JQuery code looks like the following:

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

When I look in Fiddler, I receive a 200 status code as expected. The request header shows:

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

What am I doing wrong? Why are latitude and longitude always 0 in the operation code? Thank you!

If they're strings why isn't the json in quotes?

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

as opposed to

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

Can you either change your json to enclose the values in quotes or change your operation to accept double s?

GET requests expect the parameters like ?Param=Value

You could switch your OperationContract to WebInvoke and your JS service call to POST

Or if you want to stick with GET you can just remove the stringify call and jQuery should take care of making the GET parameters correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM