标签: angular4-httpclient

Angular HttpClient默认和特定请求超时

通常,希望具有将应用于所有请求的默认超时(例如30秒),并且可以针对特定的更长请求(例如600s)覆盖.

Http我所知,没有很好的方法来指定默认超时.

有什么方法可以解决这个问题HttpClient?如何为所有传出请求定义默认超时,可以覆盖特定的超时?

angular angular4-httpclient

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

错误错误:StaticInjectorError(AppModule)[UserformService - > HttpClient]:

在尝试添加PrimeNG表时,我在这里打破了我的构建:https://github.com/BillyCharter87/Tech-O-Dex-UI/tree/BrokeIt

我记得将我package.json从TypeScript 2.3.4 更新到2.4.0并且由于(我认为)我正在使用HeadersHttp我的POST调用而破坏了它.我尝试将其设置回2.3.4无济于事.我已经修改了我可以添加的内容:

import { HttpClient, HttpHeaders } from "@angular/common/http";
Run Code Online (Sandbox Code Playgroud)

但仍然遇到我现在的错误HttpClient.我尝试HttpClient像这样导入提供程序:providers: [HttpClient]for app.module.ts.

完整错误如下:

AppComponent.html:9 ERROR Error: StaticInjectorError(AppModule)[HttpClient -> HttpHandler]: 
StaticInjectorError(Platform: core)[HttpClient -> HttpHandler]: 
NullInjectorError: No provider for HttpHandler!
Run Code Online (Sandbox Code Playgroud)

angular angular4-httpclient

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

Angular 4:"(未知URL)的Http失败响应:0未知错误"

我正在尝试使用API​​ URL.我用以下代码调用API.

import {HttpClient, HttpClientModule, HttpParams} from "@angular/common/http";

@Injectable()
export class PropertyPrefService {

    constructor(private http: HttpClient,
                private configurationService:Configuration) {}

    public searchProjects(propFilter:string):any{
        let temp:any;
        const options = propFilter ?
        {
            params: new HttpParams().set('query', propFilter)
        } : {};

        return this.http.get<any>(this.configurationService.propertySystemApiUrl, options)
                    .subscribe((results:any) => {
                        console.log(results);
                    });
    }
Run Code Online (Sandbox Code Playgroud)

在Angular代码中,我没有得到任何响应,并且收到错误:

(未知网址)的Http失败响应:0未知错误".

但是,当我在Chrome上打开开发人员工具时发出请求时,我看到响应是从服务器收到的.

URL是"https:// ..."URL而不是"http:// ...".

https httpclient typescript angular angular4-httpclient

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

如何使用Angular 4.3 HttpClient在post请求的正文中发布字符串?

我们有一个.net WebAPI,它在post请求的主体中查找文件路径字符串并返回相应的图像.我正在努力使用新的httpClient成功地从Angular 4.3传递一个字符串.可能吗?其他东西正在使用端点,所以我真的不想创建一个字符串的'模型',所以我基本上可以复制它,但如果可能的话,将它传递给json.

WebAPI方法签名:

public HttpResponseMessage ImagesFromPaths(HttpRequestMessage request, [FromBody] string path)
Run Code Online (Sandbox Code Playgroud)

现行服务方式:

getImage(path: string): Observable<any> {

  return this.http.post(
    `${apiUrl}`,
    { path },
    { headers: new HttpHeaders({
      'Content-Type': 'application/json',
    }), responseType: 'blob',
  })
}
Run Code Online (Sandbox Code Playgroud)

要成为一件容易的事吗?

angular angular4-httpclient

14
推荐指数
2
解决办法
4849
查看次数

将 Typescript“Map”作为 Angular4 HTTP POST 请求的一部分传递

我有一个 Angular 4 项目,我在其中创建了一个示例地图,如下所示:

let sampleMap = new Map<string, string>();
sampleMap.set('key1','value1');
Run Code Online (Sandbox Code Playgroud)

现在我将此 Map 作为参数传递给 Angular 4 Post 方法,该方法连接到 Spring Boot Rest 后端,如下所示

角度 4 代码:

this.http.post('http://localhost:8080/data/posturl', sampleMap).map((response: Response) => {
      return <string>response.text();
    })
Run Code Online (Sandbox Code Playgroud)

Spring Boot Rest后端代码:

@RequestMapping("/posturl")
public String launch(@RequestBody Map<String, String> sampleMap) {
    System.out.println("Received=" + sampleMap);
    return "SUCCESS";
}
Run Code Online (Sandbox Code Playgroud)

尽管当我尝试打印如上所示的“sampleMap”时,它会打印一张如下所示的空白地图:

Received={}
Run Code Online (Sandbox Code Playgroud)

我正在使用 Typescript 版本 '~2.3.3' 并且我的 'tsconfig.json' 提到目标为 'es5'。有人可以解释这种行为吗?

typescript angular angular4-httpclient

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

multi:true表示angular4中的提供者

HTTP_INTERCEPTORS在angular4中使用.为此,我创建HttpServiceInterceptor了实现HttpInterceptor接口的类并为intercept方法提供了定义.然后注册提供商HTTP_INTERCEPTORS这样

providers: [{
    provide: HTTP_INTERCEPTORS,
    useClass: HttpServiceInterceptor,
    multi: true
}],
Run Code Online (Sandbox Code Playgroud)

这工作正常.但我仍然不明白multi:true这里的意思是什么?我读过这个答案.

如果我删除multi:true选项,则浏览器端出现错误

Uncaught Error: Provider parse errors:
Mixing multi and non multi provider is not possible for token InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1
    at NgModuleProviderAnalyzer.webpackJsonp.487.NgModuleProviderAnalyzer.parse (vendor.js:36335)
    at NgModuleCompiler.webpackJsonp.487.NgModuleCompiler.compile (vendor.js:43184)
    at JitCompiler.webpackJsonp.487.JitCompiler._compileModule (vendor.js:51527)
    at vendor.js:51472
    at Object.then (vendor.js:26354)
    at JitCompiler.webpackJsonp.487.JitCompiler._compileModuleAndComponents (vendor.js:51470)
    at JitCompiler.webpackJsonp.487.JitCompiler.compileModuleAsync (vendor.js:51399)
    at PlatformRef_.webpackJsonp.0.PlatformRef_._bootstrapModuleWithZone (vendor.js:4746)
    at PlatformRef_.webpackJsonp.0.PlatformRef_.bootstrapModule (vendor.js:4732)
    at Object.<anonymous> …
Run Code Online (Sandbox Code Playgroud)

angular-providers angular angular4-httpclient

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

如何解决使用 Angular 4 检测到的循环依赖?

我使用的是 Angular 4 简单表单,使用命令行编译项目时出错,如何解决警告。参见错误图1https : //i.stack.imgur.com/N1AH4.png

>     WARNING in Circular dependency detected:
>     src\app\shopinfo\shopinfo.component.ts -> src\app\shopinfo\shopinfo.module.ts ->
> src\app\shopinfo\shopinfo.routing.ts ->
> src\app\shopinfo\shopinfo.component.ts
Run Code Online (Sandbox Code Playgroud)

component.ts: 下面是我的component.ts,这里声明component.html值的表单值

  [import {Component, OnInit, ViewEncapsulation, ElementRef, OnDestroy} from '@angular/core';
    import {FormGroup, FormControl, Validators} from "@angular/forms";
    import {IOption} from "ng-select";
    import {ShopInfo} from './shopinfo.module';    
    @Component({
      selector: 'app-shopinfo',
      templateUrl: './shopinfo.component.html',
      styleUrls: \['./shopinfo.component.css'\]
    })    
    export class shopinfoComponent implements OnInit {
      myForm: FormGroup;
      shopinformation: any;
      submitted: boolean;
      constructor(private shopinfo: ShopInfo) {
        let shopname = new FormControl('', Validators.required);
        let …
Run Code Online (Sandbox Code Playgroud)

spring-boot angular angular4-forms angular4-httpclient

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

仅针对特定服务的拦截器

我的应用程序中有一些服务指向不同的API URL。现在,我需要为每个服务设置不同的标头。我的问题现在是关于Angular 4中的新拦截器。是否可以为特定服务设置一个拦截器?那么每个服务都有其特定的拦截器?

希望你们能回答我的问题。

angular4-httpclient

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

ASP.NET Core 2.0和Angular 4.3文件上传进度

使用新的Angular 4.3 HttpClient,如何在向客户端上报上传进度的同时上传和访问ASP.NET Core 2.0 Controller中的文件?

asp.net-core asp.net-core-2.0 angular angular4-httpclient

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

415(不支持的介质类型)角4柱

我正在尝试使用angular 4 post方法访问一个wep api。

在我的服务中,我添加了application / json的内容类型。我在将数据发送到api时将对象转换为json。我正在使用HttpClientModule

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()

export class NewServiceService {

  baseUrl = "http://localhost:33969/api/";
  headers = { headers: new Headers({ 'Content-Type': 'application/json' }) 
      };
  obj= {
    name:"A",
    cgpa: 3
  };

_http:any;
constructor(http: HttpClient) {
    this._http = http;
}

SaveStudents(){

    this._http
    .post(
        this.baseUrl + 'home/Save', 
        JSON.stringify(this.obj),
        this.headers
     )
  .subscribe(
    res => {
      alert("Student Saved!");
    },
    err => {
      alert("Error!");
    }
  );
}}
Run Code Online (Sandbox Code Playgroud)

在API中,

using Entity;
using Microsoft.AspNetCore.Mvc;
using …
Run Code Online (Sandbox Code Playgroud)

asp.net-core-webapi angular4-httpclient

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