标签: angular-http

$ http从服务到控制器的POST响应

如何在下面的案例中获得Service的响应?

服务:

app.factory('ajaxService', function($http) {
    updateTodoDetail: function(postDetail){
        $http({
            method: "POST",
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            url: post_url,
            data: $.param({detail: postDetail})
        })
        .success(function(response){
            //return response;
        });
    }
})
Run Code Online (Sandbox Code Playgroud)

控制器:

updated_details = 'xyz';
ajaxService.updateTodoDetail(updated_details);
Run Code Online (Sandbox Code Playgroud)

在上面这种情况下,我通过控制器POST数据,它工作正常,但现在我希望响应进入我的控制器.

如何实现?

angularjs angular-http

6
推荐指数
1
解决办法
1万
查看次数

$ http和$ q有什么区别?

  • a)$http和之间有什么区别$q
  • b)什么时候应该$q实施$http,反之亦然?
  • c)在和实施最佳做法$http,并$q在同一时间?

angularjs angular-http angular-services angular-promise

6
推荐指数
1
解决办法
6453
查看次数

如何以角度扩展$ http服务?

不幸的是,我们的运行速度为1.2.26(当它被宝石化时会升级到1.2.28).

在此期间,我如何修补(heh)$ http以便可以使用简写patch方法?我对整个服务/工厂/模块的事情都很陌生.我已经做了几个小时的搜索,似乎无法弄明白.

myApp.factory('patchedHTTP', function($http, BasicService) {
  // $http['patch'] = function(url, data, config) {
  //   return $http(angular.extend(config || {}, {
  //     method: 'patch',
  //     url: url,
  //     data: data
  //   }));
  // };
  var extended = angular.extend(BasicService, {});
  extended.createShortMethodsWithData('patch');
  return extended;
});
Run Code Online (Sandbox Code Playgroud)

以上是我得到的最好的......它没有做任何XD

angularjs angular-http

6
推荐指数
1
解决办法
2388
查看次数

在Angular 4中从HttpClient缓存数据

我有一个问题,使我的缓存更简单.我认为有更好的方法来做到这一点.我的问题是我必须在每个get()函数中执行此"缓存"代码,这会导致代码更长.有谁帮助如何做到这一点最好的方式?谢谢.这是我的代码如下.我在我的代码中做的是我在news.service.ts中执行get()函数以从http获取数据,并在我的新闻列表中订阅它.

news.service.ts

getAllNews() {

    if(this.newslist != null) {
      return Observable.of(this.newslist);
    } 

    else {

      return this.httpClient
        .get('http://sample.com/news')
        .map((response => response))
        .do(newslist => this.newslist = newslist)
        .catch(e => {
            if (e.status === 401) {
                return Observable.throw('Unauthorized');           
            }

        });
    }
  }
Run Code Online (Sandbox Code Playgroud)

新闻list.service.ts

 this.subscription = this.newsService.getAllNews()
      .subscribe(
        (data:any) => {
          console.log(data);
          this.newslists = data.data.data;
        },
        error => {
          this.authService.logout()
          this.router.navigate(['signin']);
        });
  }
Run Code Online (Sandbox Code Playgroud)

caching angular-http angular-http-interceptors angular

6
推荐指数
1
解决办法
9361
查看次数

Angular 4 Http POST无效

我希望每个人都做得很好.我最近开始使用angular 4.4,我一直试图将数据发布到我的api服务器,但不幸的是它没有用.我花了两天时间,但仍然没有成功.并且已经尝试过6-7篇甚至来自angular.io的文章.我已经尝试了HttpHttpclient模块,但似乎没有任何工作.

问题是,每当我尝试将数据发布到我的服务器时,Angular都会生成http OPTIONS类型请求而不是 POST.

this.http.post('http://myapiserver.com', {email: 'adam@example.com'}).subscribe(
    res => {
        const response = res.text();
    }
);
Run Code Online (Sandbox Code Playgroud)

我也试图发送请求的自定义选项,但仍然没有成功.

const headers = new Headers({ 'Content-Type': 'x-www-form-urlencoded' });
const options = new RequestOptions({ headers: headers });
options.method = RequestMethod.Post;
options.body = {name: 'Adam Smith'};
//options.body = JSON.stringify({name: 'Adam Smith'}); // i also tried this
//options.body = 'param1=something&param2=somethingelse'; // i also tried this
Run Code Online (Sandbox Code Playgroud)

我使用的是ASP.NET核心2.0,但由于它不起作用我也试过简单的PHP代码,这里是php的服务器端代码.它也没有用.

<?php
print_r($_POST);
?>
Run Code Online (Sandbox Code Playgroud)

注意:Cors也在服务器上启用.另外,我也试过简单的get请求,它的工作非常好.

我真的很感激一些帮助.

提前致谢

javascript http-post angular-http angular angular-httpclient

6
推荐指数
2
解决办法
3万
查看次数

http响应对象中没有方法

$ http.get返回的对象没有方法.示例:我有我的班级模型

export class Lab {

    constructor(
        public id: number,
        public name: string,
        public description: string,
        public isActive: boolean,
        public classes: Classes[]
    ) { }

    isActive(lab: Lab) {
        return this.isActive;
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的服务中,我打电话给http取实验室

getLab(labId: number) {
    return this.http.get<Lab>(DidacticsServiceUrls.apiRoot + labId).toPromise();
}
Run Code Online (Sandbox Code Playgroud)

当我在某个组件中得到它时,方法isActive是未定义的,所以调用

lab.isActive();
Run Code Online (Sandbox Code Playgroud)

抛出异常.这有什么清洁的解决方案吗?

angular-http angular

6
推荐指数
1
解决办法
1161
查看次数

如何在 XmlHttpRequest 中使用 Angular 拦截器

我正在使用 Angular 4。还为任何 http 请求使用了 http 拦截器。https://angular.io/api/common/http/HttpInterceptor

但我使用 xmlHttpRequest 的发布请求之一用于某些文件上传事件。标头在 http post 请求(this.http.post(...))中设置正确。但是在 xmlHttpRequest 中,没有调用拦截器。所以不能设置标题。我如何处理 xmlHttpRequest(post 方法)中的 http 拦截器?

interceptor angular-http angular

6
推荐指数
1
解决办法
1858
查看次数

为什么AngularJS将X-XSRF-TOKEN标头作为JSON字符串发送?

Angular将X-XSRF-TOKEN标头设置为XSRF-TOKENcookie 的值:

var xsrfValue = isSameDomain(config.url, $browser.url())
                ? $browser.cookies()[config.xsrfCookieName || defaults.xsrfCookieName]
                : undefined;
if (xsrfValue) {
  headers[(config.xsrfHeaderName || defaults.xsrfHeaderName)] = xsrfValue;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果XSRF-TOKEN使用$cookieStore(例如,对于Rails集成)设置cookie :

$cookieStore.put("XSRF-TOKEN", "my_token"); 
Run Code Online (Sandbox Code Playgroud)

cookie存储为JSON字符串:

put: function(key, value) {
  $cookies[key] = angular.toJson(value);
}
Run Code Online (Sandbox Code Playgroud)

这意味着标题将具有额外的双引号:

X-XSRF-TOKEN    "my_token"
Run Code Online (Sandbox Code Playgroud)

为什么Angular fromJson()在设置标头的值时不会调用,以便标头看起来像这样:

X-XSRF-TOKEN    my_token
Run Code Online (Sandbox Code Playgroud)

这样可以避免我们删除服务器端的额外双引号.

我错过了一些明显的东西吗?

注意:我不是在寻找解决方法.我试图了解这种行为是否是预期的行为,如果是的话,理由是什么?

angularjs angular-http angular-cookies

5
推荐指数
1
解决办法
5548
查看次数

使用Angular.js从Web服务获取数据

我试图使用Angular从远程WS获取Json格式的数据,我遇到了一些麻烦.数据来自Web服务正确,但我不能在控制器内使用它.这是为什么?角度代码:

var booksJson;
var app = angular.module('booksInventoryApp',[]);

// get data from the WS
app.run(function ($http) {
    $http.get("https://SOME_API_PATH").success(function (data) {
        booksJson = data;
        console.log(data);  //Working
    });
});

app.controller('booksCtrl', function ($scope) { 
    $scope.data = booksJson;
    console.log($scope.data); //NOT WORKING
});
Run Code Online (Sandbox Code Playgroud)

HTML:

<section ng-controller="booksCtrl">
<h2 ng-repeat="book in data">{{book.name}}</h2>
</section>
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-scope angular-http

5
推荐指数
1
解决办法
5万
查看次数

Angular HTTP:状态-1和CORS

在我们的角度应用程序中,有时我们会将http状态-1返回给我们.状态-1发生在关闭的弹出窗口上,因此用户不受其影响,只影响我们的日志.

我试图通过这样做来处理它

      switch (response.status) {
        case 0:
          break;
        case -1:
          break;
        case 401:
          localStorageService.clearAll();
          redirectToUrlAfterLogin.url = $location.path();
          $location.path('/login');
Run Code Online (Sandbox Code Playgroud)

AngularJS问题#12920中提出了这一点

我们肯定会减少登录,但仍然有一些HTTP -1状态代码.有没有不同的方法我应该处理-1?

http http-status-codes cors angularjs angular-http

5
推荐指数
1
解决办法
2867
查看次数