Отправить запрос на Asp.net WebService с помощью Angular - PullRequest
0 голосов
/ 28 сентября 2019

Я выполнил WebService с помощью Почтальона, и он выполняется правильно.А теперь я хочу отправить запрос в WebService с помощью Angular

. Вот веб-служба Asp.net

. Это код моего WebService

public class Temperature : System.Web.Services.WebService
    {
        [WebMethod]
        public double Farenheit(double celsius)
        {
            return  (celsius * 9) / 5 + 32;
        }
        [WebMethod]
        public double Celsius(double fahrenheit)
        {
            return (fahrenheit - 32) * 5 / 9;
        }
    }

Иэто скриншот того, как я отправляю запрос с помощью PostMan, и он работает как положено

Снимок экрана для вызова WebService с использованием Postman

Вот код для Angular

const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'text/xml',

      })
    };


this.http.post('https://localhost:44389/Temperature.asmx/Celsius', '50',  httpOptions)
    .subscribe(res => {
      console.log('Data: ' + res);
    })

И вот эта ошибка, которую я получаю

Ошибка

"System.InvalidOperationException: Request format is invalid: text/xml.
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
"

Сообщение

Http failure response for https://localhost:44389/Temperature.asmx/Celsius: 500 OK

Имя

"HttpErrorResponse"

Ответы [ 2 ]

1 голос
/ 28 сентября 2019

попробуйте это:

import { HttpHeaders } from '@angular/common/http';

setHttpHeader() {
 const headers = new HttpHeaders().set('Accept', 'application/json').set('Content-Type', 'application/json');
let options = { headers: headers };
return options;
}


this.http.post('https://localhost:44389/Temperature.asmx/Celsius', '50', this.setHttpHeader())
.subscribe(res => {
  console.log('Data: ' + res);
})
0 голосов
/ 28 сентября 2019

Измените 'Content-Type': 'text / xml' на 'Content-Type': 'application / json'

...