简体   繁体   English

Asp.net WebService 无法将“System.Int32”类型的 object 转换为 ty...neric.IDictionary`2[System.String,System.Object]'

[英]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 请求集的截图

enter image description here在此处输入图像描述

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.

相关问题 ASP.NET FormView:“类型'System.Int32'的对象不能转换为类型'System.String” - ASP.NET FormView: “Object of type 'System.Int32' cannot be converted to type 'System.String” ASP.NET Core 2:2 错误无法将“System.Int32”类型的对象转换为“System.String”类型。 在登录过程中 - ASP.NET Core 2:2 error Unable to cast object of type 'System.Int32' to type 'System.String'. on login process 无法将“ System.Object []”转换为类型“ System.String []” - Cannot convert 'System.Object[]' to the type 'System.String[]' 类型'System.Int32'的表达式不能用于方法'Boolean Equals(System.Object)'的'System.Object'类型的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' 类型'System.Int32'的表达式不能用于方法'Boolean Equals(System.Object)'的类型'System.Object'的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' 类型'System.Int32'的表达式不能用于返回类型'System.Object' - Expression of type 'System.Int32' cannot be used for return type 'System.Object' Linq和Equality Operator:类型'System.Int32'的表达式不能用于'System.Object'类型的参数 - Linq and the Equality Operator: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' 错误无法将类型为“ System.Int32”的对象转换为类型为“ System.String”的对象 - Error Unable to cast object of type 'System.Int32' to type 'System.String' InvalidCastException:无法将“System.String”类型的对象转换为“System.Int32”类型 - InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32' C# 无法将“system.string”类型的对象转换为“system.int32”类型 - C# unable to cast object of type 'system.string' to type 'system.int32'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM