[英]Asp.net WebService Cannot convert object of type 'System.Int32' to ty…neric.IDictionary`2[System.String,System.Object]'
I've been trying to call a WebService using Angular我一直在尝试使用 Angular 调用 WebService
I already tested the WebService and Send a request using Postman and it worked correctly.我已经使用 Postman 测试了 WebService 并发送请求,它工作正常。
Here is the code for the WebService这是WebService的代码
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
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;
}
}
And this is my Angular Code这是我的 Angular 代码
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
})
};
this.http.post('https://localhost:44389/Temperature.asmx/Celsius', 50, httpOptions)
.subscribe(res => {
console.log('Data: ' + res);
})
The Method Accept an Int data type.方法接受 Int 数据类型。 And I'm passing
50
integer.我正在传递
50
integer。 So why does I'm receiving this error.那么为什么我会收到此错误。 I don't understand Why
我不明白为什么
ExceptionType: "System.InvalidOperationException"
Message: "Cannot convert object of type 'System.Int32' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'"
StackTrace: " at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
↵ at System.Web.Script.Serial
And here is the screenshot of the POSTMAN requset这是 POSTMAN 请求集的截图
You need to pass:你需要通过:
{fahrenheit: 50}
rather than:而不是:
50
This is because your existing code passes up a number but doesn't indicate which parameter it maps to.这是因为您现有的代码传递了一个数字,但没有指出它映射到哪个参数。 By explicitly mentioning
fahrenheit
the server-side is able to map the 50
value to the appropriate method parameter.通过明确提及
fahrenheit
温度,服务器端能够将50
值设置为适当的方法参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.