使用新的Angular 4.3 HttpClient,如何在向客户端上报上传进度的同时上传和访问ASP.NET Core 2.0 Controller中的文件?
constructor(private http: HttpClient) {
}
ngOnInit() {
this.http.get('url').subscribe(data => {
console.log(data);
console.log(data.login);
});
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我可以在控制台中看到带有登录名的数据,但是却收到一条错误消息,指出数据中不存在属性登录名。有没有使用界面的解决方法吗?因为我想要获取的数据非常庞大,并且将来可能会被修改,所以还有更好的方法吗?
我正在尝试使用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) 我有一个认证HttpInterceptor:
import {HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor,
HttpRequest} from '@angular/common/http';
import {AuthService} from '../service/auth.service';
import {Observable} from 'rxjs/Observable';
import {Injectable} from '@angular/core';
import {Router} from '@angular/router';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private authService: AuthService,
private router: Router) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authHeader = this.authService.getToken();
const clone = req.clone({headers: req.headers.set('Authorization',authHeader)});
return next.handle(clone).do(() => {
}, err => {
console.log(err);
if (err instanceof HttpErrorResponse && err.status === 401) {
this.authService.clearToken();
this.router.navigate(['/auth/signin']);
return Observable.empty();
} …Run Code Online (Sandbox Code Playgroud) 我在角度4中使用隐式授权流,建议的方法是将windows.location.href设置为身份验证提供程序的url。
我是否可以从windows.location.href创建一个Promise,以便我知道一旦Promise返回就触发后续操作?
如果没有承诺,还有其他选择吗?
我必须处理大量文件上传(超过4GB)。目前,我正在使用新的HttpClient。是否可以在Angular 4.3的新HttpClient模块中为分块上载设置选项?我找到了带有ng-file-upload模块的Angularjs解决方案。不幸的是,它不适用于角度4。谢谢您的帮助。
file-upload chunks chunked-encoding angular angular4-httpclient
我开始学习Angular 4并使用HTTP Client进入部分.现在我正在尝试从组件进行http调用(是的,我知道我应该将其转移到服务,但仍然)
但由于某种原因,当我尝试将HttpClient注入我的组件时,我得到下一个错误:
未捕获错误:无法解析PromocodeComponent的所有参数:(?).
这是我的组件的代码:
import { Ticket } from '../../classes/Ticket.class'
import { Component, Input } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'promocode',
templateUrl: './promocode.template.html',
styleUrls: ['./promocode.styles.scss']
})
export class PromocodeComponent {
@Input() ticket: Ticket;
state: String = "normal";
promocode: String = "";
constructor(private http: HttpClient) {}
promocodeValidate(event): void{
console.log(this.promocode);
console.log(event);
this.http.get('/promocode/asdasda').subscribe(data => {
console.log(data);
});
}
}
Run Code Online (Sandbox Code Playgroud)
我的app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } …Run Code Online (Sandbox Code Playgroud) 我在我的一个项目中使用 Angular 4,我有一种动态创建表单元素并提交它的方法
postToUrl(path, params, method) {
method = method || 'post';
let form = document.createElement('form');
form.setAttribute('method', method);
form.setAttribute('action', path);
for (let key in params) {
if (params && params.hasOwnProperty(key)) {
let hiddenField = document.createElement('input');
hiddenField.setAttribute('type', 'hidden');
hiddenField.setAttribute('name', key);
hiddenField.setAttribute('value', params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
setTimeout(() => {
document.body.removeChild(form);
}, 2000);
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是为请求设置一个自定义标头,以便我可以附加服务器所需的所有标头。有什么方法可以为此编写一个通用拦截器,这样我就不必重复这些行。请帮忙。任何帮助表示赞赏。
如何使用Angular 4 HttpClient拦截器实现加载程序?我已经完成登录并附加了授权标头,并且我想知道是否在此过程中正在加载某些数据时是否也可以执行“加载器功能”?我想附加一个图标加载器,用于指示是否正在加载某些数据。顺便说一下,这是我的authinterceptor。
export class GithubAuthInterceptor implements HttpInterceptor {
intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authReq = req.clone({
headers: req.headers.set('Authorization', 'token')
});
return next.handle(authReq);
}
}
Run Code Online (Sandbox Code Playgroud) 在这篇文章在这里和其他人:看起来你需要导入app.module.ts HttpClientModule和HttpClient的在app.component.ts做一个HTTP请求.为什么?他们是如何工作的?