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!
GETrequests expect the parameters like?Param=ValueYou could switch your
OperationContracttoWebInvokeand your JS service call toPOSTOr if you want to stick with
GETyou can just remove thestringifycall and jQuery should take care of making theGETparameters correctly.