http - 如何从angular2中的WebMethod获取数据

标签 http angular

在 Javascript 中使用 Ajax

   $.ajax({
    type: "Get",
    url: "AttendanceMaster.aspx/GetDistinctBatch",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    datatype: "jsondata",
    async: "true",       
    success: function(response) {

    },
    error: function(response) {
        alert("(allstudent )" + response.status + ' Error ' + response.statusText);
    }
   });

尝试跟随 angular2

  return this._http.get("AttendanceMast.apsx/GetDistinctBatch")
  .map((response) => response.toString());

返回

{ "_isScalar": false, "source": { "_isScalar": false }, "operator": {} }

C#

 [System.Web.Services.WebMethod]
 public static string GetDistinctBatch()
 {      
    return "welcome Angular 2";
 }

1)在angular2中如何使用?

查看下面的错误

enter image description here

最佳答案

您的方法只是返回 Observable,它们将只是一个函数。 Observable 本质上是惰性的。在您订阅它们之前,它们不会被执行。

AttendanceMast() {
   return this._http.get("AttendanceMast.apsx/GetDistinctBatch")
         .map((response) => response.toString()); //better do it response.json();
}

myService.AttendanceMast().subscribe(
  data => console.log(data);
)

关于http - 如何从angular2中的WebMethod获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41436483/

相关文章:

http - 优雅地关闭多台服务器

apache - 为什么我的新 lets encrypt ssl 设置会收到浏览器警告?

angular - Angular Material 2 步进控件

当服务函数用作输入参数时,Angular Service 的依赖项不会被注入(inject)

Angular 2 Http超时

html - 如何将 ionic 图标放置在 ionic HTML 移动用户界面的文本字段中?

http - google.com 的 golang 反向代理

c++ - 如何发送 POST 请求并接收 GET 响应

java - 响应中的 JAX/Jersey 自定义错误代码

angular - 从 Angular 2 中的指令调用组件的方法