私たちはCore Web APIにAjaxリクエストを行い、さらに処理するためにdata
Json結果をコントローラに戻そうとしています.Jsonオブジェクト結果の逆シリアル化、Ajaxリクエストが正常に機能しています。必要なJsonデータはdata
です。Ajax Jsonオブジェクトを.Net CoreのControllerクラスに渡すにはどうすればいいですか?
これを達成するための変更点や代替方法についてアドバイスをいただけますか?
ビュー(Ajaxリクエスト)
@section scripts
{
<script type="text/javascript">
$(document).ready(function() {
GetEventDetails('@Url.Content("~/")');
});
function GetEventDetails(contextRoot) {
$.ajax({
type: "GET",
url: contextRoot + "api/EventsAPI",
dataType: "json",
success: function (data) {
debugger;
var datavalue = data;
contentType: "application/json";
//Send data to controller ???
console.log(data);
},
error: function (xhr) {
alert(xhr.responseText);
}
});
}
</script>
}
Controller.cs
public ActionResult Index()
{
/*Do all stuff before returning view
1. Get Json Data from Ajax request
2. Deserialize the obtained Json Data
3. return to view
*/
return View("Index");
}
更新:
私は@Jによって与えられた解決策を試してみました。 Doeが、結果セットを取得できませんでした。
public ActionResult Index(RootObject model)
{
if (model != null)
{
return Json("Success");
}
else
{
return Json("An Error Has occoured");
}
//return View("Index");
}
:
ここ
public class RootObject
{
public Embedded _embedded { get; set; }
public Links _links { get; set; }
public Page page { get; set; }
}
は、アクション結果のコントローラーです:
function GetEventDetails(contextRoot) {
$.ajax({
type: "GET",
url: contextRoot + "api/EventsAPI",
dataType: "json",
success: function (data) {
debugger;
var datavalue = data;
contentType: "application/json; charset=utf-8";
console.log(data);
var EventJson = data;
console.log(EventJson);
$.ajax({
type: "POST",
url: "@Url.Action("Index")",
dataType: "json",
data: EventJson,
contentType: "application/json; charset=utf-8",
//data: EventJson,
success: function (data) {
alert(data.responseText);
console.log('Data received: ');
console.log(data.responseText);
},
failure: function (errMsg) {
console.log(errMsg);
}
});
},
error: function (xhr) {
alert(xhr.responseText);
}
});
}
クラスのプロパティ:スクリーンショットとコードの下..
Ajaxリクエストをご確認ください
エラースナップショット:
が更新質問をご確認ください:JSでJSONオブジェクトを作成する方法
3)。 –