如何从JavaScript传递对象到Windows运行时组件c#

How to pass object from JavaScript to Windows Runtime Component C#?

本文关键字:Windows 运行时 组件 对象 JavaScript      更新时间:2023-09-26

我确实有以下对象参数在JavaScript中,我想把它传递给c# Windows运行时组件的Windows应用程序。

var param = {
        "url" : "http://192.168.101.224/DEMO/All/DemoService.svc/login",
        "requesttype" : "POST",
        "paramss" : {
            "userName" : "demouser",
            "password" : "abcdef",
            "domain" : "demodomain",
            "accessKey" : "12345"
        }
    }
.....
callWebservice: function (param, callBack) {
        try {
            service = new ServcieRuntimes.Service();
            service.callHttpService(param).then(function (data) { ... )};

在c#, WinRT组件类中,我是这样做的。

 public IAsyncOperation<string> CallHttpService(string param)
    {
        return CallHttpServiceHelper(json).AsAsyncOperation();
    }
 private async Task<string> CallHttpServiceHelper(string param)
    {
        try
        { ....... }

但是我得到

[object Object]
在c#。

请帮我一下。提前感谢!

你的c#代码需要一个(大概是JSON)字符串,你需要提供一个

service.callHttpService(JSON.stringify(param)).then(function (data) { ... )};

当前得到

[object Object]

因为这是{}.toString()的输出;