如何从angular js ajax调用asp.net webservice方法

时间:2015-12-16 17:50:58

标签: c# asp.net angularjs ajax

我想通过uisng angularjs ajax概念从webservice.asmx.cs类中调用一个字符串方法来显示一个弹出消息uisng alert方法。以下是我所拥有的代码



 
var obj = angular.module("myapp", []);        
        obj.controller("HomeController", function ($scope, $http)
        {
            //$scope.student = [];            
                $http({
                    url: "WebService1.asmx/HelloWorld",
                    method: "GET",
                    data: "",
                    headers: { "Content-Type": "application/json", 'dataType': 'json' }
                })
                .success(function (response) {                    
                    alert(resp.d);

                });
            });

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>


<body ng-app="myapp">
    <h1>Sample Application</h1>    
    <div ng-controller="HomeController">
      
    </div>
</body>
&#13;
&#13;
&#13;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebApplication1
{ 

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

    [WebMethod]
    public static string HelloWorld()
    {
        return "Hello World";
    }
}
}

最后,当我运行上面的代码时,它不起作用。它既没有显示任何警报信息也没有任何错误。它只显示带有该h1标签的空白窗口。需要帮助!

0 个答案:

没有答案