标签: angular-http

AngularJS $ http.post服务器不允许使用方法OPTIONS的请求

我正在尝试对服务器进行HTTP POST.

我必须发送的数据是一个json对象.

问题是$ http.post在角度覆盖带有选项的方法.

我可以做这个配置

.config(['$httpProvider', function ($httpProvider) {
  //Reset headers to avoid OPTIONS request (aka preflight)
  $httpProvider.defaults.headers.common = {};
  $httpProvider.defaults.headers.post = {};
  $httpProvider.defaults.headers.put = {};
  $httpProvider.defaults.headers.patch = {};
}])
Run Code Online (Sandbox Code Playgroud)

并从选项更改为POST,但我无法将内容类型设置为"application/json",并且我收到"415不支持的媒体类型"

谢谢

angularjs angular-http

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

AngularJS全局$ http状态ajax加载器

我有一个AngularJS应用程序,需要一个ajax加载器来处理$ http所做的每个请求 - 有一个简单的方法来做到这一点.

我现在的解决方案是每次调用$ http时设置$ rootScope.loading = 1,并在成功设置$ rootScope.loading = 0 ..

这是什么"最佳实践"?

我的代码现在看起来像:

$rootScope.loading = 1;
$http({method : "POST", url:url, data: utils.params(data), headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).success(function() {
    $rootScope.loading = 0;
});
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-scope angular-http

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

离子.从JSON文件中的数据返回工厂

我正在玩Ionic示例项目,并且正在寻找一种从json文件中提取数据的方法,而不仅仅是项目中的标准数组.

我已经成功修改了services.js以从JSON中获取数据,但我的模板没有获取数据.我假设这是因为它在JSON的http请求完成之前执行.

我需要做些什么来完成这项工作?

........
.factory('People', function($http) {
  // Might use a resource here that returns a JSON array
  var people = $http.get('../data/peopleData.json').success(function(response){
    console.log(response.people); //the data is correctly logged
    return response.people;
  });

  // var people = [{
  //   id: 0,
  //   name: 'Kassiopi'
  // }, {
  //   id: 1,
  //   name: 'Imerola Bay'
  // }];
  //original and works great
return {
    all: function() {
      return people;
    },

    get: function(personId) {
      for (var i = 0; i < people.length; i++) {
        if …
Run Code Online (Sandbox Code Playgroud)

angularjs angular-http ionic-framework

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

angularjs $ http:在路径参数中发送特殊字符

我正在尝试调用REST服务,该服务在其路径参数中具有特殊字符.其余网址看起来像,

rest\fetchDetails\[1,2,3]
Run Code Online (Sandbox Code Playgroud)

我只是使用$ http来调用它,

$http.get('rest\fetchDetails\[1,2,3]');
Run Code Online (Sandbox Code Playgroud)

它不起作用,当我使用firebug调试它时,我可以看到url被发送为

rest\fetchDetails\%5B1,2,3%5D
Run Code Online (Sandbox Code Playgroud)

有没有办法在调用$ http调用时发送'['和']'?

angularjs angular-http

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

Angular 2 Http响应通用错误处理程序

我正在实现一个Angular 2应用程序,它将进行一些API调用.我想知道是否还有一个通用的错误处理程序.例如,如果API以401代码响应,我想将用户重定向到登录路由.这是真的吗?

谢谢

angular-http angular

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

Angular 2 observable-subscribe显示未定义

我有同样的挑战是在SO后的面孔在这里,我的订阅方法越来越不确定我component.ts,即使在我的服务,我有数据.请参阅p.component.ts下面的代码

 private getPayItems():void{
    console.log('In getPayItems');
    this._payItemService.getPayItems()
    .subscribe(data => { 
        this.payItemArray = data;
        console.log(data);
    },
    (error:any) =>{
         this.alerts.push({ msg: error, type: 'danger', closable: true }); 
    }) 
}
Run Code Online (Sandbox Code Playgroud)

p.service.ts

getPayItems():Observable<Payitem[]>{

    let  actionUrl = this.url +  "/GetPayItem";

    return this._http.get(actionUrl, { headers: this.headers })
        .map((response: Response) => { 
            <Payitem[]>response.json() ;
             console.log(<Payitem[]>response.json()); //This logs the Object
        })
        .catch(this.handleError);
}
Run Code Online (Sandbox Code Playgroud)

observable rxjs typescript angular-http angular

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

从HTTP远程服务器获取Angular 2的JSON失败,但在本地成功

我正在尝试使用Angular2中的htttp.get读取JSON文件。

当我在项目上使用本地存储的文件时,它工作正常

但是-当我使用具有完全相同文件的远程服务器时,出现以下错误:

404-找不到集合“未定义”

浏览到http://example.com/my.json,我可以在任何浏览器上看到该文件。

这是我获取文件的打字稿代码:

private url = '../my.json';   <--- This works
private url = 'http://example.com/my.json';   <--- This throws the error


return this.http.get(this.url)
                    .map(this.extractData)
                    .catch(this.handleError);
Run Code Online (Sandbox Code Playgroud)

提前致谢。

angular-http angular

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

为Angular 4中的每个Http请求创建全局身份验证标头

当我从api得到一些东西时,我想让授权标题不要一次又一次地声明它.

每当我需要从api获取数据时,我需要附加授权标头.我目前在Angular 4中使用HTTPCLIENT.我的代码:

auth.service.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import 'rxjs/add/operator/map';
import { AppSettings } from '../app.constants';


@Injectable()
export class AuthService {
  private loggedIn = false;

  constructor(private httpClient: HttpClient) {
  }

  loginUser(email: string, password: string) {  
    const headers = new HttpHeaders() 
    .set('Content-Type', 'application/json');

    return this.httpClient
    .post(
       GLOBAL_URL.LOGIN_URL + '/auth/login', 
       JSON.stringify({ email, password }), 
       { headers: headers }
    )
    .map(
        (response: any) => {
         localStorage.setItem('auth_token', response.token);
          this.loggedIn = true;
          return response;
        });
   } …
Run Code Online (Sandbox Code Playgroud)

angular-http angular-http-interceptors angular angular-httpclient

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

在angular2的何处以及如何使用HttpResponse

HttpResponseclass(在@ angular / common / http中)是@ angular / http(不推荐使用)的类Response的替换。看文档并没有太多关于如何使用它的想法!此外,我试图替换旧的角度代码,但由于此类是通用的,因此需要类型,例如。赋予它类型将产生错误,如:HttpResponse<T>

Property 'json' does not exist on type 'HttpResponse<any>'

谁能帮我知道如何在Angular中使用HttpResponse类吗?

更新

这是我编写的代码段,即函数“ get”:

get(path: string, params: HttpParams = new HttpParams()): Observable<any> {
  return this.http.get(`${environment.api_url}${path}`, { headers: this.setHeaders(), search: params })
  .catch(this.formatErrors)
  .map((res: HttpResponse<any>) => res.json());
Run Code Online (Sandbox Code Playgroud)

angular-http angular

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

ASP.NET Core Angular 7 Http发布-需要复杂对象吗?

我正在尝试向ASP.NET Core后端进行基本的HTTP发布。

角度:

var parms = {
    userRoles: ['User', 'Worker'],
    email: 'email@email.com'
};

this.http.post('/UserRole/SaveUserRoles', parms)
    .subscribe(result => { });
Run Code Online (Sandbox Code Playgroud)

控制器:

[HttpPost]
public async Task<IActionResult> SaveUserRoles([FromBody]string[] userRoles, string email)
{
    return Ok();
}
Run Code Online (Sandbox Code Playgroud)

我的参数显示为空。我可以通过创建一个复杂的对象C#端来使其正常工作,但这不是我想要的。我不需要仅用于2个参数的对象。

这里缺少什么?

c# angular-http asp.net-core angular

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