C# オブジェクトとして読み取ることができる JSON オブジェクトを作成しようとしています。AJAX 経由で送信しようとすると、400 の不正な要求エラーが発生し続けます。
私のJavaScriptは次のとおりです。
$.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 オブジェクトは次のようになります。
function Fixture() { }
Fixture.prototype =
{
ID: "",
HomeTeam: "",
AwayTeam: "",
Date: "",
toJSON: function () { return { "fixture": {"ID" : this.ID, "HomeTeam" : this.HomeTeam, "AwayTeam" : this.AwayTeam, "Date" : this.Date} }; }
};
このアプローチは、私が作成した他の関数ではまったく問題なく機能しているため、何が問題になっているのかわかりません。
私の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# クラス:
[DataContract(Name="Fixture", Namespace="")]
public class Fixture
{
[DataMember(Name = "ID", Order = 1)]
public int ID { get; set; }
[DataMember(Name = "HomeTeam", Order = 2)]
public Team HomeTeam { get; set; }
[DataMember(Name = "AwayTeam", Order = 3)]
public Team AwayTeam { get; set; }
[DataMember(Name = "Date", Order = 4)]
public DateTime Date { get; set; }
}
[DataContract(Name = "Team", Namespace = "")]
public class Team
{
[DataMember(Name = "ID", Order = 1)]
public int ID { get; set; }
[DataMember(Name = "Name", Order = 2)]
public string Name { get; set; }
[DataMember(Name = "Wins", Order = 3)]
public int Wins { get; set; }
[DataMember(Name = "Losses", Order = 4)]
public int Losses { get; set; }
[DataMember(Name = "Draws", Order = 5)]
public int Draws { get; set; }
[DataMember(Name = "GoalsConceded", Order = 6)]
public int GoalsConceded { get; set; }
[DataMember(Name = "GoalsScored", Order = 7)]
public int GoalsScored { get; set; }
[DataMember(Name = "Manager", Order = 8)]
public Manager Manager { get; set; }
}
[DataContract(Name = "Manager", Namespace = "")]
public class Manager
{
[DataMember(Name = "ID", Order = 1)]
public int ID { get; set; }
[DataMember(Name = "Name", Order = 2)]
public string Name { get; set; }
}
Manager クラスと Team クラスは、JavaScript の Fixture クラスとまったく同じ方法で記述されます。
オブジェクトは正しい JSON だと思っていたものにシリアル化されますが、これらのエラーが発生した場合は明らかにそうではありません。生成される JSON は次のとおりです。
{ "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なし)は次のとおりです。
<?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>