简体   繁体   English

415-不支持的媒体类型

[英]415 - Unsupported Media Type

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

import { Event, SearchParameters } from './event.model';

@Injectable()
export class EventService {    
    private _url = "http://localhost:36888/api/HealthFairEvents";

    constructor(private _http: Http) {

    }

    getEvents(SearchParameters) {        
        return this._http.post(this._url + "/HealthFairEventSL", SearchParameters).map(res => res.json());
    }

    getEvent(id: number) {        
        return this._http.get(this._url + "/GetHealthFairEvent/" + id).map(res => res.json());
    }

    addEvent(event: any){
        console.log('add');
        console.log(JSON.stringify(event));
        return this._http.post(this._url + "/PostHealthFairEvent", JSON.stringify(event)).map(res => res.json());
    }

    updateEvent(event: Event){
        console.log('update');        
        console.log(JSON.stringify(event));
        return this._http.put(this._url + "/PutHealthFairEvent/" + event.HealthFairEventId, JSON.stringify(event), 

        ).map(res => res.json());
    }
}

it look like I need to send the 'Access-Control-Allow-Origin:*' or CORS with the _http request. 看来我需要使用_http请求发送“ Access-Control-Allow-Origin:*”或CORS。 but I am not able to do it. 但是我做不到。 I checked the Angular4 documentation but no success. 我检查了Angular4文档,但没有成功。 Please help me thanks. 请帮我谢谢。

CORS happens on the server side and the only headers necessary on the client side is the content type. CORS发生在服务器端,而客户端唯一需要的标头是内容类型。

Example: 例:

import { Http, Response, Headers, RequestOptions } from '@angular/http';

getEvent(id: number) {
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });
        return this._http.get(this._url + "/GetHealthFairEvent/" + id, options).map(res => res.json());
    }

On the server side, to enable CORS there are several ways to do this. 在服务器端,要启用CORS,有几种方法可以做到这一点。 The easiest is to add this to your web.config after installing the Microsoft.AspNet.WebApi.Cors package from NuGet. 最简单的方法是从NuGet安装Microsoft.AspNet.WebApi.Cors软件包 后, 将其添加到web.config中

  <system.webServer>
   <httpProtocol>
     <customHeaders>
        <add name="Access-Control-Allow-Origin" value="http://localhost:4200" />
        <add name="Access-Control-Allow-Methods" value="*" />
        <add name="Access-Control-Allow-Credentials" value="true" />
     </customHeaders>
   </httpProtocol>
</system.webServer>

More detailed information about CORS from Microsoft: https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api Microsoft提供的有关CORS的更多详细信息: https : //docs.microsoft.com/zh-cn/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api

Correct answer is, 正确的答案是,

updateEvent(event: HealthFairEvent){
        console.log('update');        
        console.log(JSON.stringify(event));
        let customHeader = new Headers();
        customHeader.append('Access-Control-Allow-Origin', '*');

        return this._http.put(this._url + "/XX/" + event.HealthFairEventId, event, { headers: customHeader } ).map(res => res.json());        
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM