jsonを返すwebservicesを作成したいと思います。しかし、私は常にレスポンスのコンテンツタイプとして「text/html」を取得しています。asp.net webAPIがいつもtext/htmlを返すのはなぜですか?
最初のショット:
public StringContent Get()
{
List<Cell> list = new List<Cell>();
Cell c = new Cell("Cell1");
Cell c2 = new Cell("Cell2");
list.Add(c);
list.Add(c2);
return new StringContent(
Newtonsoft.Json.JsonConvert.SerializeObject(list),
Encoding.UTF8,
"application/json");
}
Responsecontent:
System.Net.Http.StringContent
セカンドショット:
public List<Cell> Get()
{
Cell c = new Models.Cell("Cell1");
List<Cell> list = new List<Cell>();
list.Add(c);
return list;
}
Responsecontent:System.Collections.Generic.List`1【でTestApp .Models.Cell]
これは私がエンドポイントにアクセスする方法である:
$.ajax({
url: "http://localhost:54787/Cell/Get",
type: "GET",
contentType:"application/json",
accepts: {
text: "application/json"
},
success: function (response) {
$("#result").html(JSON.parse(response));
},
error: function (xhr, status) {
alert("error");
}
});
エンドポイントをどのようにテストしていますか? – zulq
私は '/ Cell/Get'をchromeから呼び出しています。私もjQuery $ .ajax get Requestを試して、chromeのネットワークモニタをチェックしました。 – user66875
あなたはこれを見ましたか? http://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome?rq=1 – zulq