2016-12-30 1 views
0

私はAJAXとC#を初めて使用していますので、一緒に作業する方法を試しています。私はWebアプリケーションを構築しています。私はajax関数でC#のメソッドを呼び出しています。すべて正常に動作すれば、私はC#クラスに成功メッセージを返します。どうやってやるの? これは私のAjaxの方法C#でajaxレスポンスを返すには?

<script type="text/javascript"> 


Hello(); 
    function Hello() { 
     $.ajax({ 
      type: "POST", 
      url: "Dtata.aspx/Hello", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (result) { 
       response(result.d); 
      }, 
      error: function (result) { 
       alert('There is a problem processing your request'); 
      } 
     }); 
    } 


</script> 

    //Basically I want to know the success/failure value from the Ajax call and print it back to my console. 

    protected void Page_Load(object sender, EventArgs e) 
    { 


    } 

    [WebMethod] 
    public static string Hello(string name) 
    { 
     return name; 
    } 
+0

を私はあなたが逃したと思いますあなたのajax呼び出しで文字列名を渡すために.. $ .ajax({ タイプ: "POST"、 url: "Dtata.aspx/Hello"、 contentType: "application/json; charset = utf-8 "; データ型:" json "、データ:{名前: 'hello'} 成功:関数(結果){ 応答(result.d); }、 エラー:関数(結果){ アラート( 'あなたのリクエストを処理する際に問題があります'); } }); –

+0

循環リクエスト/応答を作成しようとしています。 C#Webメソッドから送信された応答は、AJAXの成功につながります。これをサーバーに再送しようとしています。 –

答えて

0

では、あなたのAJAX呼び出しでも、データを渡すためにしようとしましたか? (文字列名)..あなたのバックエンド・サービス(WebMethod属性)を引き起こすそのメソッドのparamでそれを持っている...のようなもの:

<script type="text/javascript"> 




     Hello(); 
      function Hello() { 
       $.ajax({ 
        type: "POST", 
        url: "Dtata.aspx/Hello", 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        data:{name:'hello'}, 
        success: function (result) { 
         response(result.d); 
         Counter() //<-- CALL OTHER AJAX METHOD TO INCREASE COUNTER ON BACK END 
        }, 
        error: function (result) { 
         alert('There is a problem processing your request'); 
        } 
       }); 
      } 


function Counter() { 
       $.ajax({ 
        type: "POST", 
        url: "Dtata.aspx/Counter", 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 

        success: function (result) { 
         console.log(result.d); 
        }, 
        error: function (result) { 
         alert('There is a problem processing your request'); 
        } 
       }); 
      } 


     </script> 

      //Basically I want to know the success/failure value from the Ajax call and print it back to my console. 
     private int _counter; 
      protected void Page_Load(object sender, EventArgs e) 
      { 


      } 

      [WebMethod] 
      public static string Hello(string name) 
      { 
       return name; 
      } 



      [WebMethod] 
       public static int Counter() 
       { 
        this._counter = this._counter +1; 
        return this._counter 
       } 
+0

:ありがとうございます。しかし、私はどのように私のC#クラスにajaxコールから成功/失敗の応答を印刷するのですか? –

+0

申し訳ありません。あなたがしたいことを正確に理解していません。レスポンスを印刷しますか? ...それをバックエンドに送って他のWebメソッドに戻しますか? –

+0

: - 私の究極の目標は、私のC#クラスにカウンタがあることです。カウンタは、データがc#からajaxメソッドに渡されるたびにインクリメントされ、メソッドは「成功」応答を返します。カウンタは、応答が成功した場合にのみ増加します。ですから、私はC#クラスでカウンタ値を増やすことができるように、Ajaxレスポンスを読み取る方法を見つけなければなりません。 –

0

をあなたはAJAX呼び出しにデータを逃した:

var counter=0; 
$.ajax({ 
    type: "POST", 
    url: "Dtata.aspx/Hello", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    data:{name:"test"} 
    success: function (result) { 
      //result is response from c#. you can do your trick here 
      alert(result.d); 
      if(result.d=="success"){ 
       counter++; 
      } 
    }, 
    error: function (result) { 
      alert('There is a problem processing your request'); 
    } 
}); 
+0

: - カウンタ値をC#クラスに戻すにはどうすればよいですか?カウンタ値は配列インデックスとして送信され、残りの作業が行われます。だから、カウンタ値をC#クラスに返す方法を見つけなければなりません。 –

+0

別のajaxメソッドをc#class –

+0

にカウンタ値を送るように呼び出すことができます: - この時点で私は本当に紛失して混乱しているので、どうすればいいか教えてください。 –

関連する問題