2017-02-14 8 views
0

webmethodにパラメータを送信するためにtriyingしています。私はそれが私のものではないので、私は変更のWebメソッドを傾ける。私はそれをjqueryだけで使う必要があります。jqueryからwebメソッドにパラメータを送信するにはどうすればいいですか?

Webメソッドの例。

[WebMethod] 
public static string getValue(int id, out string name) 
{ 
    name = (id * 5).ToString(); 
    return "Success"; 
} 

Jquery例;

$(document).ready(function() { 
    getValue(); 
}); 

function getValue() { 
    $.ajax({ 
     url: "../WebMethods.aspx/getValue", 
     type: 'post', 
     contentType: "application/json", 
     dataType:"json", 
     data: JSON.stringify({ id: 0, name: "" }), 
     success: function (data) { 
      console.log(data); 
     } 
    }); 
} 

私は成功関数に名前の値が必要です。出来ますか?私はC#とその働きでそれを呼び出すことができます。私はそれをajaxメソッドで動作させる必要があります。

答えて

0

成功方法で使用できます。あなたのケースのための

$(this).attr('data') 

それは次のようにする必要があります:

$(document).ready(function() { 
getValue(); 

});

function getValue() { 
    $.ajax({ 
     url: "../WebMethods.aspx/getValue", 
     type: 'post', 
     contentType: "application/json", 
     dataType:"json", 
     data: JSON.stringify({ id: 0, name: "" }), 
     success: function (data) { 
      console.log(data); 


     console.log($(this).attr('data')); 
     } 
    }); 
} 

---更新2

この

var Data = { id: 0 , name : "" }; 

function getValue() { 
    $.ajax({ 
     url: "../WebMethods.aspx/getValue", 
     type: 'post', 
     contentType: "application/json", 
     dataType:"json", 
     data: Data , 
     success: function (data) { 
      console.log(data); 


     console.log(Data.id); 
     } 
    }); 
} 
+0

そのhtmlのない要素、doesntの仕事 – cbalakus

+0

を試してみてください、私はそれを試してみてください。.. :) –

+0

データdidntの変更をもう一つの溶液を添加しています。名前は私が送信するものです – cbalakus

関連する問題