2017-03-29 7 views
0

私たちは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リクエストをご確認ください

エラースナップショット:

enter image description here

答えて

0

1)返信先のコントローラを[FromBody]に追加します。これにより、コントローラはJSONオブジェクトを期待します。

[HttpPost] 
public ActionResult Index([FromBody]RootObject model) 
    { 
     if (model != null) 
     { 
      return Json("Success"); 
     } 
     else 
     { 
      return Json("An Error Has occoured"); 
     } 
     //return View("Index"); 
    } 

2)これは、あなたが正しいJSONオブジェクトを掲示していない動作しない場合は/あなたはJSONオブジェクトを使用すると、ブラウザのコンソールに渡しているオブジェクトを検査するconsole.dir(EventJson);を使用して投稿されていません。 https://www.w3schools.com/js/js_json_stringify.asp

0

我々は

こんにちはソ連sojuzしようとしています!

しかし、バックの質問に - 2ヶ月前、私はネットのコアを持つAjaxでGitHubの上でサンプルを作成した - 要するにhttps://github.com/CShepartd/ASP.Net_Core_-_Ajax_Example

コントローラ内のオブジェクトでアクションを行います。

public IActionResult Ajax(string name1, string name2) 
    { 
     return View(); 
    } 

次は、name1name2を入力してください。

$('form').submit(function(event) { 
      event.preventDefault(); 
      var data = { 
       name1: $("input[name='name1']", this).val(), 
       name2: $("input[name='name2']",this).val() 
      }; 

      $.ajax({ 
       type: 'POST', 
       url: '/Home/Ajax', 
       dataType: 'json', 
       data: data, 
       success: function(response) { 
        alert(response); 
        console.log('Data received: '); 
        console.log(response); 
       }, 
       failure: function(response) { 
        //... 
       }, 
       error: function(response) { 
        //... 
       } 
      }); 
     }); 
+0

が更新質問をご確認ください:JSでJSONオブジェクトを作成する方法

3)。 –

関連する問題