Код: Выделить всё
OPTIONS http://localhost:9090/api/values 401 (Unauthorized)
Failed to load http://localhost:9090/api/values: Response for preflight has invalid HTTP status code 401.
Web.config:
Код: Выделить всё
Код: Выделить всё
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace api.Controllers
{
[Authorize]
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
}
Код: Выделить всё
constructor(private myService: MyService) {
this.myService.myPost({ID: 1, FirstName: 'Bob'})
.subscribe(
data => console.warn(data),
err => console.error(err),
() => console.log("empty")
);
}
Код: Выделить всё
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse, HttpRequest, HttpHeaders, HttpInterceptor, HttpHandler, HttpEvent, HttpParams} from '@angular/common/http';
import { Observable } from 'rxjs';
import { from } from 'rxjs';
import { map, filter, catchError, mergeMap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class MyService {
constructor(private http: HttpClient) {
};
public myPost(body) {
const httpOptions = {
withCredentials: true
}
return this.http.post("http://localhost:9090/api/values", body, httpOptions);
}
}
MyService.ts
Код: Выделить всё
public myPost(body) {
const httpOptions = {
headers: new HttpHeaders({
'Authorization': '?????'
}),
withCredentials: true
}
return this.http.post("http://localhost:9090/api/values", body, httpOptions);
}
Подробнее здесь: https://stackoverflow.com/questions/515 ... -authentic