2016-09-20 6 views
0

をGET WEBAPIにデータを送信します呼び出すよう: - メソッドは呼び出されますが、取得したSampleClassオブジェクトが 富栄nullである私はWEBAPIでモデルクラスの下に持って要求Ajax呼び出し

var source = { 
       'id': 0, 
       'Name': 'sagar' 
      }    

      $.ajax({ 
       type: "GET", 
       dataType: "json", 
       url: "http://localhost:51366/api/Values", 
       data: source, 
       success: function (data) { 
        alert(data); 
       }, 
       error: function (error) { 

        jsonValue = jQuery.parseJSON(error.responseText); 
        alert("error" + error.responseText); 
       } 
      }); 

t私はidを0、Nameをsagarとして送信しました。 nullの代わりにこれらの値を取得する必要があります。

+2

[HttpGet]身体が含まれていないためアクセスできない場合は、[HttpPost] –

答えて

3
  1. マークHttpPostとサーバーメソッド

    enter image description here

  2. "POST" あなたのデータ
  3. サーバーで

...クライアント上

[HttpPost] 
public string Get([FromBody]SampleClass sample) 

...

  $.ajax({ 
       type: "POST", 
       dataType: "json", 
       url: "http://localhost:51366/api/Values", 
       data: source, 
       success: function (data) { 
        alert(data); 
       }, 
       error: function (error) { 

        jsonValue = jQuery.parseJSON(error.responseText); 
        alert("error" + error.responseText); 
       } 
      }); 

EDITは - 非推奨:

はこのようなクエリ文字列に入れて、あなたが「HTTPGET」サーバーにデータを送信できるようにするために体の必要性を避けるために...

$.ajax({ 
        type: "GET", 
        dataType: "json", 
        url: "http://localhost:51366/api/Values?id=" + source.id + "&Name=" + source.Name, 
        success: function (data) { 
         alert(data); 
        }, 
        error: function (error) { 

         jsonValue = jQuery.parseJSON(error.responseText); 
         alert("error" + error.responseText); 
        } 
       }); 
+0

を使用する必要がありますが、このHTTPGETが必要です –

+0

HTTPプロトコルではGETしてもデータを本文に投稿できませんそんなにうまくいくわけではありませんが、大丈夫ですか? – War

関連する問題