c# - 获取 html 作为响应而不是从 asmx 服务返回的对象

标签 c# asp.net angularjs asmx angularjs-http

我正在尝试使用 AngularJS $http 调用 asmx 服务。但我无法从网络服务中获取数据。我得到一个成功回调,但响应是整个 html 页面而不是作为响应返回的对象。

这是我的 C# asp.net web 服务代码:

public class DataService : System.Web.Services.WebService
{

    [WebMethod]
    public Hello HelloWorld()
    {
        Hello h = new Hello();
        h.Message = "Hello World";
        return h;
    }
}

public class Hello
{
    public string Message { get; set; }
}

这是 AngularJS 代码

    function FormController($scope,$http) {

    $scope.submit = function (user) {
        var url = '../../Services/DataService.asmx?op=HelloWorld';
        $http({
            method: "get",
            url: url
        }).success(function (data, status, headers, config) {
            console.log(data);
        });
    }

    FormController.$inject = ['$scope','$http'];
}

上面的方法在表单提交时调用。

最佳答案

看起来 URL 不正确。

尝试:

function FormController($scope,$http) {

    $scope.submit = function (user) {
        var url = '../../Services/DataService.asmx/HelloWorld';
        $http({
            method: "get",
            url: url
        }).success(function (data, status, headers, config) {
            console.log(data);
        });
    }

    FormController.$inject = ['$scope','$http'];
}

关于c# - 获取 html 作为响应而不是从 asmx 服务返回的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24323437/

相关文章:

javascript - AngularJS:我的 Accordion 展开,但不折叠

javascript - 如何将 json 文件加载到 Angularjs 中以进行 ng-repeat

c# - 如何使 ViewModel 验证与模型验证保持同步?

C# 等效于 Java 中的 ByteArrayOutputStream

c# - 刷新数据透视表 EPPlus

asp.net - 使用 html-webpack-plugin 将捆绑脚本标签注入(inject)带有母版页的 <asp :Content> tag in legacy . aspx 文件中?

c# - 通过 asp.net 访问 Office Word 对象模型导致 "failed due to the following error: 80070005 Access is denied."

angularjs - Angular 在 ui-select2 上的双向绑定(bind)

c# - 无法从 Decimal 转换为 Int32

javascript - 将 javascript 函数 Hook 到每个回发