C# 创建可以在C中反序列化的JSON对象#
我试图创建一个JSON对象,它可以作为C#对象读取,当我试图通过AJAX发送它时,我不断收到400个错误的请求 我的JavaScript如下所示:C# 创建可以在C中反序列化的JSON对象#,c#,javascript,jquery,json,wcf,C#,Javascript,Jquery,Json,Wcf,我试图创建一个JSON对象,它可以作为C#对象读取,当我试图通过AJAX发送它时,我不断收到400个错误的请求 我的JavaScript如下所示: $.ajax({ type: "POST", url: "LeagueService.svc/Fixtures", data: JSON.stringify(fixture), dataType: "json", contentType: "application/json", success: function (j
$.ajax({
type: "POST",
url: "LeagueService.svc/Fixtures",
data: JSON.stringify(fixture),
dataType: "json",
contentType: "application/json",
success: function (json, status, req) {
$successMessage = '<div class="hero-unit"><h2>Success</h2><p>The fixture has been added to the database.</p></div>';
$('#dialog').html($successMessage).fadeOut(500, function (e) {
$(this).dialog("close");
});
},
error: function (req, status, error) {
alert('Error: ' + req.responseText);
}
});
{ "fixture": {
"ID":0,
"HomeTeam":{
"ID":1,
"Name":"Rhondda St FC",
"Wins":21,
"Losses":2,
"Draws":4,
"GoalsConceded":20,
"GoalsScored":36,
"Manager": {
"ID":1,
"Name":"Ron Burgundy"
}
},
"AwayTeam": {
"ID":2,
"Name":"Mt Pleasant United FC",
"Wins":18,
"Losses":5,
"Draws":3,
"GoalsConceded":22,
"GoalsScored":28,
"Manager": {
"ID":2,
"Name":"Brian Fantana"
}
},
"Date":"02/19/2013 12:00pm"
}}
这种方法在我编写的其他函数中工作得非常好,所以我不确定现在出了什么问题
我的C#方法和接口:
接口:
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/Fixtures")]
void AddFixture(Fixture fixture);
方法是:
public void AddFixture(Fixture fixture)
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.DBConnect))
{
try
{
conn.Open();
string query = "INSERT INTO tbFixtures (HomeTeam, AwayTeam, Date) VALUES(@HT, @AT, @Date);";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@HT", fixture.HomeTeam.ID);
cmd.Parameters.AddWithValue("@AT", fixture.AwayTeam.ID);
cmd.Parameters.AddWithValue("@Date", fixture.Date);
cmd.ExecuteNonQuery();
}
}
catch (Exception) { }
finally
{
conn.Close();
}
}
}
以及C#类:
Manager和Team类的编写方式与JavaScript中Fixture类的编写方式完全相同
该对象序列化为我认为正确的JSON,但如果我遇到这些错误,显然不会。
生成的JSON如下所示:
$.ajax({
type: "POST",
url: "LeagueService.svc/Fixtures",
data: JSON.stringify(fixture),
dataType: "json",
contentType: "application/json",
success: function (json, status, req) {
$successMessage = '<div class="hero-unit"><h2>Success</h2><p>The fixture has been added to the database.</p></div>';
$('#dialog').html($successMessage).fadeOut(500, function (e) {
$(this).dialog("close");
});
},
error: function (req, status, error) {
alert('Error: ' + req.responseText);
}
});
{ "fixture": {
"ID":0,
"HomeTeam":{
"ID":1,
"Name":"Rhondda St FC",
"Wins":21,
"Losses":2,
"Draws":4,
"GoalsConceded":20,
"GoalsScored":36,
"Manager": {
"ID":1,
"Name":"Ron Burgundy"
}
},
"AwayTeam": {
"ID":2,
"Name":"Mt Pleasant United FC",
"Wins":18,
"Losses":5,
"Draws":3,
"GoalsConceded":22,
"GoalsScored":28,
"Manager": {
"ID":2,
"Name":"Brian Fantana"
}
},
"Date":"02/19/2013 12:00pm"
}}
如果有人能对此有所了解,我们将不胜感激。谢谢
编辑:完全错误(没有CSS)是:
请求错误
请求错误
服务器在处理请求时遇到错误。请参阅有关构造对服务的有效请求的说明
我认为这是因为它不喜欢你约会的格式,即“2013年2月19日12:00pm”。尝试将C#Fixture数据契约中的日期类型更改为字符串,或编辑JS Fixture原型的toJSON方法,以返回一个字符串,格式如“2013-02-19Z”.什么是完整的错误消息?在末尾添加了错误消息。您是否尝试过调试您的服务方法?我发现请求从未到达该方法,这使我相信服务本身不是问题。您能否在服务器上启用跟踪,以了解服务为什么认为请求不好?另外,您是否看到帮助页面(如错误页面中建议的)以与您的请求进行比较?我尝试更改日期,但没有成功。最终,我只是将Fixture C#类更改为保存团队ID而不是团队对象,现在它可以工作了。这并不是我想做的,但很管用。
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Request Error</title>
</head>
<body>
<div id="content">
<p class="heading1">Request Error</p>
<p xmlns="">The server encountered an error processing the request. Please see the <a rel="help-page" href="http://localhost:55310/LeagueService.svc/help">service help page</a> for constructing valid requests to the service.</p>
</div>
</body>
</html>