Question

I want to pass string value from c# to JavaScript using JSON. So I created an example plugin name: Echo.cs (in CordovaWP namespace), and an "echo" method in Echo class. Like this tutorial.

In index.js, I called:

 cordova.exec(function (result) 
        {
            alert("OK");
        }, function (error) {
            alert("KO");
        }, "CordovaWP.Echo", "echo", "ok");

But I can't get debug in echo method. And have nothing found!

Was it helpful?

Solution

Use as below:

cordova.exec(function (result) 
    {
        alert("OK");
    }, function (error) {
        alert("KO");
    }, "CordovaWP.Echo", "echo", ["ok"]);

Parameters should always be sent as an array from JS to cs

Please can you also post your CS code:

Check sample below SMS example:

JS:

    var sendSMS = function(phoneNumber,smsBody){
    cordova.exec(function(){console.log("success SMS");},function(){console.log("Error SMS");},"SMS", "sendSMS", [phoneNumber,smsBody]);
};

CS:

 namespace Cordova.Extension.Commands{
   public class SMS : BaseCommand
    { 
        public void sendSMS(string arg)
        {
            string recipient = JsonHelper.Deserialize<string[]>(arg)[0];
            string smsBody = JsonHelper.Deserialize<string[]>(arg)[1];
            SmsComposeTask composeSMS = new SmsComposeTask();
            composeSMS.Body = smsBody;
            composeSMS.To = recipient;
            composeSMS.Show();
            this.DispatchCommandResult();
        }

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top