I am developing a WindowsPhone App using Apatch Cordova plugin for WindowsPhone.
I need to pass argument from the C# layer to the JavaScript one, and I do it using the PluginResult in the c# layer with formatted string, and with JSON.parse() function in the CordovaCommandResult plugin of the JavaScript layer ( cordova.js file. )
However, when trying to pass string values an exception occurs "Syntax Error: invalid character".
How can I pass string value thus the JSON.parse() will parse it successfully?
Here is my code:
c# layer:
public void GetConfiguration(string sensorName)
{
PluginResult result = new PluginResult(PluginResult.Status.OK,GetConfiguration());
DispatchCommandResult(result);
}
public override string GetConfiguration()
{
string config = String.Format("\"unit\":{0},\"exponent\":{1},\"frequency\":{2}",
unit,
exponent.ToString("0.0", CultureInfo.InvariantCulture),
frequency.ToString("0.0", CultureInfo.InvariantCulture));
return "{" + config + "}";
}
Where unit is a string variable, and it causes the exception.
JavaScript layer: cordova.js
Calling the GetConfiguration function:
getConfiguration: function (successCallback, errorCallback, args) {
var win = function (result) {
successCallback(result);
};
var fail = errorCallback && function (code) {
errorCallback(code);
};
exec(win, fail, "SensorsManager", "GetConfiguration", args);
},
Parsing the result returned from the c# layer:
define("cordova/plugin/windows8/CordovaCommandResult", function (require,exports,
module) {
var cordova = require('cordova');
var channel = require('cordova/channel');
// singular WP callback function attached to window,
//status is used to determine if it is a success or error
module.exports = function (status, callbackId, args, cast) {
if (status === "backbutton") {
// do not detach backbutton event, as we need to be able to catch exceptions
cordova.fireDocumentEvent("backbutton", undefined, true);
return "true";
}
else if (status === "resume") {
cordova.fireDocumentEvent('resume');
return "true";
}
else if (status === "pause") {
cordova.fireDocumentEvent('pause');
return "true";
}
var parsedArgs;
try {
parsedArgs = JSON.parse(args);
}
catch (ex) {
return;
}
var safeStatus = parseInt(status, 10);
if (safeStatus === cordova.callbackStatus.NO_RESULT ||
safeStatus === cordova.callbackStatus.OK) {
cordova.callbackSuccess(callbackId, parsedArgs, cast);
}
else {
cordova.callbackError(callbackId, parsedArgs, cast);
}
};
});