0

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

1 Answer 1

1

Actually the JSON that you are creating is not proper.First validate the json that you have prepared using jsonlint.com

More point: 1) What is the content type that you are sending back in response.It will be better if you use third party library to create Json and then directly send content type as text/json from the server.

Sign up to request clarification or add additional context in comments.

4 Comments

What do you mean when you say my JSON has a wrong format - you mean the string I return from the GetConfiguration function is not proper?
ya it is not in proper json format.You simply take that string and validate it using jsonlint.com
Ok. i got it. in the JSON string - every string value must be surrounded with quotes for example: "x": "foo" and not "x": foo like I had origionally.
Also send other values also in json format. unit, exponent.ToString("0.0", CultureInfo.InvariantCulture), frequency.ToString("0.0", CultureInfo.InvariantCulture)); .I think they are also not in json format.

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.