【问题标题】:Unable to call webservice function angularjs无法调用webservice函数angularjs
【发布时间】:2016-10-14 18:47:31
【问题描述】:

无法从 angularjs 控制器调用 webservice 函数。 它正在访问文件,但功能不可访问。 错误:

  1. 服务器响应状态为 500(内部服务器错误)
    1. System.InvalidOperationException:Web 服务方法名称无效。在 System.Web.Services.Protocols.HttpServerProtocol.Initialize() 在 System.Web.Services.Protocols.ServerProtocolFactory.Create(类型类型, HttpContext 上下文,HttpRequest 请求,HttpResponse 响应, 布尔值&中止处理)
namespace AngularJSTesting.App{

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService2 : System.Web.Services.WebService
    {

        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["TextItConnectionString"].ToString();



        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string FetchData()
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("select UserName, Name, Email, PhoneNumber from new_users where UserId < 11 order by UserId asc ", con))
                {
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                    Dictionary<string, object> row;
                    foreach (DataRow dr in dt.Rows)
                    {
                        row = new Dictionary<string, object>();
                        foreach (DataColumn col in dt.Columns)
                        {
                            row.Add(col.ColumnName, dr[col]);
                        }
                        rows.Add(row);
                    }
                    return serializer.Serialize(rows);
                }
            }
        }

        [WebMethod]
       // [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string read()
        {
            return "test";
        }
    }
    }

js文件

'use strict';

app.controller('aboutController', function ($scope, $http) {
$scope.message = "Now viewing About!";

var url = 'WebService2.asmx/read';
$http.get(url)
           .success(function (data) {
               alert("success");
               $scope.users = data;

           })
           .error(function (data) {
               alert("error");
               alert(data);
           })
});

【问题讨论】:

    标签: javascript c# angularjs asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    您需要将Script Method Get Attribute [ScriptMethod(UseHttpGet = true)] 添加到网络方法中。 同时从函数签名中删除static

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public string read()
    {
        return "test";
    }
    

    【讨论】:

    • @MuhammadTauseen 您是否尝试删除 static 关键字
    • 是的。找到了解决方案;不部署就无法访问webservice的功能
    • 无话可说。您应该首先考虑它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 2011-12-23
    相关资源
    最近更新 更多