【问题标题】:`Angularjs` call to `WebMethod` with `PARAMETERS``Angularjs` 使用`PARAMETERS` 调用`WebMethod`
【发布时间】:2016-04-02 06:31:18
【问题描述】:

如何使用angularjs 将参数发布到[WebMethod]

我的 angularjs 脚本是::

app.factory('checkAvabService', function ($http) {
    return {
        checkFare: function () {
            return $http.post('onewaydomflt.aspx/checkAvab', { 

             data:{test:"hello"} //post test as parameter to web method

          })
        }
    };
});

我的网络方法是::

[System.Web.Services.WebMethod]
public static string checkAvab(string test)  // string test is not accept value "hello"
{
    string x = test;
    return x;
}

这里我没有得到 test 参数值 helloWebMethod

这段代码有什么问题。

【问题讨论】:

    标签: c# asp.net .net angularjs webmethod


    【解决方案1】:

    只需在 $http.post 调用的第二个参数中传递数据,该参数采用 JSON 格式的数据。在您传递的对象中无需再次使用data

    return $http.post('onewaydomflt.aspx/checkAvab', {test:"hello"})
    

    【讨论】:

    • 非常感谢.. Pankaj Parker。现在 webmethod 接受参数很好......再次感谢
    • @JBNizet 感谢大佬提醒.. 但是我可以知道在将任何数据发布到 ASP.Net MVC -4 操作时,为什么我们需要以字符串格式传递请求正文。只是好奇..
    • 我对 ASP.Net MVC -4 一无所知,也不确定您所说的“字符串化格式”是什么意思。
    • @JBNizet 我的意思是说,在将数据发送到服务器之前,我将其字符串化(JSON.strigify(data))..
    • 我不明白这有什么用。 Angular 默认将传递给 $http 的对象转换为 JSON:docs.angularjs.org/api/ng/service/$http#default-transformations 如果请求配置对象的 data 属性包含对象,则将其序列化为 JSON 格式。
    猜你喜欢
    • 2023-03-12
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 2011-11-07
    • 1970-01-01
    • 2015-08-27
    相关资源
    最近更新 更多