2016-08-02 13 views
0

私はasp.net mvc5プロジェクトを開発中です。webapiを使用して別のサーバーからデータを取得していますので、以下のコードを使用してサーバーからデータを読み取ります。asp.net mvc5のアクションから返されたjsonを表示するにはajaxを使用する方法

public async System.Threading.Tasks.Task<string> webapicheap(string from,string to) 
     { 
      string url = "http://charter724.ir/webservice/list_period_date.php?from="+from+"&to="+to+"&id=367&%20key=EmlyCD8JOWX22cL8IuNBju5K5"; 
      System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); 
      System.Net.Http.HttpResponseMessage response = await client.GetAsync(url); 
      response.EnsureSuccessStatusCode(); 
      string responseBody = await response.Content.ReadAsStringAsync(); 
      // TempData["str"] = responseBody; 
      return responseBody; 
     } 

はので、私はその後responseBodyという名前の文字列は、今、私は私がこれを行う方法を知らない、この情報を使用したいビューのAJAXスクリプトに返されるので、アクションの上に呼び出すために、スクリプトの下に使用します。この行動をしてください。 WEBAPI

{"from":"\u062a\u0647\u0631\u0627\u0646","from_code":"87","to":"\u0645\u0634\u0647\u062f","to_code":"109","result":[{"week":"\u0633\u0647 \u0634\u0646\u0628\u0647","date_flight":"05\/12","price":"","link":"Result.html?t=YTo2OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDIiO3M6NDoiZnJvbSI7aToxMDAwMDtzOjI6InRvIjtpOjEwMDAxO3M6Mzoic2VjIjtpOjE0NzAxNjAxNDE7czo2OiJzZWNrZXkiO2k6MTgzMztzOjQ6InR5cGUiO3M6Mzoic3lzIjt9"},{"price":"136,000 
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/13","type_flight":"5","week":"\u0686\u0647\u0627\u0631\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDMiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"129,000 
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/14","type_flight":"5","week":"\u067e\u0646\u062c\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDQiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"120,000 
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/15","type_flight":"5","week":"\u062c\u0645\u0639\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDUiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"125,000 
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/16","type_flight":"5","week":"\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDYiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"130,000 
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/17","type_flight":"5","week":"\u06cc\u06a9\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDciO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"141,000 
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/18","type_flight":"5","week":"\u062f\u0648\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDgiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"159,000 
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/19","type_flight":"5","week":"\u0633\u0647 \u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDkiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"181,000 
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/20","type_flight":"5","week":"\u0686\u0647\u0627\u0631\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMTAiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"183,400 
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/21","type_flight":"5","week":"\u067e\u0646\u062c\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMTEiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"171,000 
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/22","type_flight":"5","week":"\u062c\u0645\u0639\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMTIiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"171,000 
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/23","type_flight":"5","week":"\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMTMiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"141,000 
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/24","type_flight":"5","week":"\u06cc\u06a9\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMTQiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"148,000 
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/25","type_flight":"5","week":"\u062f\u0648\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMTUiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"161,000 
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/26","type_flight":"5","week":"\u0633\u0647 \u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMTYiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="}],"ndate":1,"ldate":-1,"next_name":"15 \u0631\u0648\u0632 \u0628\u0639\u062f","prev_name":"15 \u0631\u0648\u0632 \u0642\u0628\u0644","path_name":"\u0645\u0633\u06cc\u0631 \u067e\u0631\u0648\u0627\u0632\u06cc "} 
+0

正確な問題は何ですか? –

+0

ラベルタグに "from"と "price"と "to"の値を表示することはできませんし、ajaxに成功関数を書き込む方法 – saba

答えて

0

あなたのJSON結果の

<script> 
    function f3() { 
      var from = $("#cou option:selected").val(); 
      var to = $("#destin option:selected").val(); 
      $.ajax({ 
       url: '/Home/webapicheap', 
       data: { from: from,to:to }, 
       type: 'post', 
       dataType: 'text', 
       success: function (data) { 
        // alert(data); 
        results = JSON.parse(data); 
        // alert(data.data) 
        //if (country_id != "" && zone_code != "" && duration != "" && birthday != "") { 
        // fPrice(data.token); 
        //} 
        $("#destin").html; 
        $.each(results, function (idx, res) { 
         alert(res.result.price) 
         $("#lbl1").append("<div>" + res.result.price + "</div>"); 
        }) 
       }, 
       error: function (x, y, z) { 
        alert('fail') 
        //alert(x.responseText); 
        //alert(z); 
       } 
      }); 

     } 

</script> 

出力はresultプロパティの下の実際の結果の配列が含まれています。 results = JSON.parse(data).result;

にラインresults = JSON.parse(data);を変更してみてください。また、ライン$("#destin").html;は何もしていない - それを削除します。

関連する問題