简体   繁体   中英

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

I do have the following object param in JavaScript and I want to pass it to C# Windows Runtime Component for Windows Apps.

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) { ... )};

In the C#, WinRT Component class, I'm doing like this.

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

 private async Task<string> CallHttpServiceHelper(string param)
    {
        try
        { ....... }

But I'm getting

[object Object]

in C#.

Please help me on this. Thanks in advance!

Your C# code expects a (presumably JSON) string, you need to supply one

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

You currently get

[object Object]

because that's the output of {}.toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM