使用Angular向Asp.net WebService发送请求

问题描述 投票:0回答:2

我使用Postman执行了WebService,并且正确执行了。现在我想使用Angular向WebService发送请求

这里是Asp.net Web服务

这是我的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发送请求并按预期工作的屏幕截图

Screenshot for Calling the WebService using 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"
asp.net angular web-services asmx
2个回答
0
投票

将您的'Content-Type':'text / xml'更改为'Content-Type':'application / json'


0
投票

尝试一下:

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);
})
© www.soinside.com 2019 - 2024. All rights reserved.