私はデータベースでselectを行いたいと思います。しかし、私はエラー(エラーの最初の行)を以下の取得:EntityFrameworkとJSONを使用したASP.NET MVCのHttpException
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below
[HttpException (0x80004005): Server cannot set content type after HTTP headers have been sent.]
System.Web.HttpResponse.set_ContentType(String value) +9752569
System.Web.HttpResponseWrapper.set_ContentType(String value) +14
System.Web.Mvc.JsonResult.ExecuteResult(ControllerContext context) +177
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +27
私DBcontrollerの一部:
string sql = @"exec [results].[GetNewResults] '" + Sources + @"', '" + Searchstring + @"', '" + username + @"', '" + sessionid + @"'";
var result = dbcontext_hke.Database.SqlQuery<UnifiedResultset>(sql);
if (results == null)
{
Debug.WriteLine("results is null");
return Json(null);
}
Debug.WriteLine("results isn't null");
Debug.WriteLine(results);
return Json(results, JsonRequestBehavior.AllowGet);
私のjsの一部:基本的に
var url = $('#initialrequest').val();
$.ajax({
url: url,
data: {sources, searchstring},
type: 'POST',
})
.success(function (result) {
for (var i = 0; i <= (9); i++) {
try {
console.log("lenght of result is :" + result.length);
add_error(result);
console.log("result is : " + result);
add_result(result[i].header, result[i].source, result[i].subsource, result[i].description);
} catch (err) {
add_error();
}
})
.error(function (xhr, status) {
$('#error').append(xhr.responseText);
console.log(xhr.responseText);
});
後いくつかの研究私はthis(おそらく)同様の問題の解決策としてそのコードを発見した:
this.Context.Response.Write(response);
this.Context.Response.Flush();
何が起こっているのか誰に教えてもらい、どうすれば解決できるのですか(または他のソリューションのコードを変更することができますか)。
EDIT1:私はコントローラが情報を取得することを保証しました。したがって、コントローラとjsの間の通信にエラーがある可能性があります。何か案は?
EDIT2:は、インデックス(時
Response.BufferOutput = true;
を追加すること)にHomeControllerとDBControllerのは何も変わりません。
EDIT3:問題がJSONの返品であることを確認しました。私は別のタイプを返す場合、それは正常に動作します。なぜこのようなことが起こり、どのように修正するのですか?
EDIT4:コマンド
var result = dbcontext_hke.Database.SqlQuery<UnifiedResultset>(sql);
はすぐに私は考えて実行していないので、私のコードは動作しない理由はあります。 Debug.WriteLine(result)の結果が何であるかを見たい場合、私はプレーンなSQLコマンドだけを取得します。アイデア?
私が言及したDBコントローラコードを超えていたライン
(http://stackoverflow.com/によって引き起こされました質問/ 2383169/http-headers-been-been-been-been-been-been-been-been-been-been-been-been-iis7-5の後にサーバーが設定できません。私はあなたのコードの他の部分があなたが示したものではなく、この動作を引き起こすと信じています。私が見る唯一の容疑者は 'return Json(null)'です - 正確にはどういうことか分かりません – Andrei
私は確信していません - 議論は概観するのは良いですが、どうやって修正するのか分かりません。 – yesfabime