我试图将Json字符串sObject从javascript传递给c#。在c#中,我试图将此JSon字符串中的数据加载到Object。 我在Javascript中收到错误请求错误。
public class StatusEntity
{
public string WorkOrderID { get; set; }
public string JobID { get; set; }
public string ClientID { get; set; }
}
[WebInvoke(UriTemplate = "/updatestatus", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
public string updateStatus(List<StatusEntity> _objStatus)
{
string sResult = string.Empty;
try
{
// SOME CODE HERE
}
catch (Exception)
{
sResult = string.Empty;
throw;
}
finally
{
}
return sResult;
}
function updateStatus() {
var sObject = '[{"WorkOrderID":"9","JobID":"48","ClientID":"9"},{"WorkOrderID":"9","JobID":"48","ClientID":"10"}]';
$.ajax({
cache: false,
type: "POST",
async: false,
url: "../JobMatchingService/updatestatus",
contentType: "application/json; charset=utf-8",
dataType: "json",
// OLD CODE - NOT WORKING
data: sObject,
// NEW CODE CHANGES WORKING
data: '{"_objStatus":' + _objStatus + '}',
success: function (response) {
alert(response);
},
error: function (xhr, options, error) {
alert(error);
}
});
}
答案 0 :(得分:0)
将_objStatus更改为方法参数中的字符串,然后使用try / catch将其转换为StatusEntity,如下所示:
var status = JsonConvert.DeserializeObject<List<StatusEntity>>(jsonString);
答案 1 :(得分:0)
对javascript进行了更改,数据必须指定如下:
// OLD CODE - NOT WORKING
data: sObject,
// NEW CODE CHANGES WORKING
data: '{"_objStatus":' + _objStatus + '}',
function updateStatus() {
var sObject = '[{"WorkOrderID":"9","JobID":"48","ClientID":"9"},{"WorkOrderID":"9","JobID":"48","ClientID":"10"}]';
$.ajax({
cache: false,
type: "POST",
async: false,
url: "../JobMatchingService/updatestatus",
contentType: "application/json; charset=utf-8",
dataType: "json",
// OLD CODE - NOT WORKING
data: sObject,
// NEW CODE CHANGES WORKING
data: '{"_objStatus":' + _objStatus + '}',
success: function (response) {
alert(response);
},
error: function (xhr, options, error) {
alert(error);
}
});
}