0

When developing a plugin, how is possible to get the result of my C# function to my javascript.

For instance having the echo plugin as in the official cordova doc

When I pass a result from C# to Javascript calling

DispatchCommandResult(new PluginResult(PluginResult.Status.OK, "{result:\"super awesome!\"}"));

Please How can I get the result inside my javascript code (how can I get: "super awesome")?

1
  • So you want to pass a variable from C# to JavaScript ? Commented Dec 18, 2014 at 16:39

2 Answers 2

1

When you dispacth the plugin result with status OK from C#, the win callback is called, and it brigns the result with this.

Example, your javascript plugin calls this from javascript:

cordova.exec(win, fail, "Echo", "echo", ["input string"]);

So you have to create a win function that will get the result:

function win(result) {
    alert(result);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could just write the string directly :

<script> var JavascriptBlah = '<%=yourString%>'</script>

or use the method InvokeScript to call a function to set the string :

webBrowser.InvokeScript("yourStringSetter", "yourString");

demo of a simple setter in js

source for the first solution

7 Comments

Please could you edit it and kindly provide an example with echo plugin?
I dont know how works echo plugin. I asked you if you want to pass a string because I know how to do that. I use the second solution. If you can execute c# and if you have wrap your application in a webbrowser you could do it too.
I can execute a method C# using Phonegap, and call it via javascript (wrapping it). Then I want to pass a variable that I obtain into this method to JS. This is possible but not very well explained here: docs.phonegap.com/en/3.5.0/….
here a full example on github
This is the line of code to call in your JS : cordova.exec(win, fail, "Echo", "echo", ["input string"]);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.