【问题标题】:How can I send out parameter to web method from jquery如何从 jquery 向 Web 方法发送参数
【发布时间】:2017-02-14 08:42:58
【问题描述】:

我正在尝试向 webmethod 发送参数。我不能改变网络方法,因为它不是我的。我只需要将它与 jquery 一起使用。

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 方法工作。

【问题讨论】:

    标签: jquery asp.net ajax out-parameters


    【解决方案1】:

    你可以用在你的成功方法中。

    $(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 );  
             }
         });
     }
    

    【讨论】:

    • 我又添加了一个解决方案,请尝试一下..:)
    • 数据没有改变。名字是我发送的
    • 它没有改变..我已经在顶部定义了它..见 var Data = { id: 0 , name : "" };
    • 我看到了,但我认为您误解了。当我发送 {id:1, name:""}。我想要名字“5”。您的数据只是局部变量。我想将此数据发送到服务器,并在不返回的情况下进行更改。我认为这是不可能的
    • 哦,对不起..是的,我误解了你的问题 :) 不,我猜这是不可能的..谢谢
    猜你喜欢
    • 1970-01-01
    • 2013-03-18
    • 2016-12-18
    • 2021-01-28
    • 1970-01-01
    • 2015-03-27
    • 2022-12-08
    • 1970-01-01
    • 2020-11-16
    相关资源
    最近更新 更多