有办法做到这一点。操作合约实际上是一个字符串。我转义了 json 字符串以将其保留为字符串。然后我在 web 方法中取消转义字符串并使用 NewtonSoftJson 将其解析为字典希望这有用...
我使用了 NewtonSoft Json 库...
c#代码
using Newtonsoft.Json;
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
public void testMethod(string jsonData)
{
string data = Uri.UnescapeDataString(jsonData);
Dictionary<string, string> x = jsonConvert.DeserializeObject<Dictionary<string, string>>(data);
foreach (KeyValuePair<string, string> kvp in x)
{
}
}
JS代码
var Data = {
width: 400,
height: 200,
someString: "somedata"
};
$.ajax({
type: "POST",
url: "Service1.svc/testMethod",
contentType: "application/json; charset=utf-8",
dataType: "json",
timeout: 1000000,
data: '{"jsonData": "' + escape(JSON.stringify(Data)) + '"}',
error: function(error) {
},
success: function (data) {
},
});