Hi,
I’d like to read values from an RPCResponse. (Without binding to a widget).
I see that the “data” parameter is returned as raw JSON.
How can I read a field?
{"response":{"status":"0","data":[{"user_id":"mike","user_name":"Mike","user_pass":"mypassword","user_level":"admin","user_email":"myaddress@test.com"}]}}
Can I use an object notation somehow?
I’d like to do something like:
isc.say(response.data[0].user_name);
Thanks,
Mike
If you set evalResult:true (or eval() that string of JSON yourself) you’ll be able to use the notation you’ve shown.
Thanks! I was able to get eval() to work.
This is part of a login form I’m writing. (Code included below).
-
It works great, but I’d like to figure out how to use evalResults instead. I had trouble getting it to work. Part of the problem is that there are several callbacks involved. Any quick changes you can suggest to make it work? I’m not sure where and when I can use the object notation.
-
Using eval(), I had trouble testing for a valid login. The first way I tried it was to check if the returned user record data was null or invalid. I had a hard time getting this to work, mostly due to my weak JavaScript fundamentals. I found all kinds of methods on the web, involving null, undefined, typeof, etc. but none worked for me.
So I simply added another field to my JSON response data, and called it “authorized”. I could set this in my PHP script, so I know I’d always have a value for it.
But is there an easier way to check for the existence of a matching user record in my back-end database, without adding an extra field like this?
- With multiple layers of callback functions, I’m searching for function names to indicate what they do, but keep them unique. Is there a “best practices” naming convention in situations like these? (i.e. other than dialogCallback1, dialogCallback2, etc.). Also, for parameter names that might be passed more than once.
Thanks,
Mike
isc.showLoginDialog(function (credentials, dialogCallback) {
// dialogCallback is a function!!
// it is our "API" to the login box
// Bail from this function if no credentials supplied
if (credentials == null) return;
// sendCredentials is a function that I implement below
// 1st parameter is the credential object (name, password) from the form
// 2nd parameter is a function for my code to call when it's done authenticating
// (the function itself is passed to my function as a parameter)
sendCredentials(
credentials,
function (loginSucceeded) {
// report success or failure to the login form
// true, it goes away
// false, it stays and shows alert
dialogCallback(loginSucceeded);
}
);
});
function sendCredentials(theCreds, theFunction) {
// Take the credentials (theCreds.username, theCreds.password)
// and send them to a PHP login script which returns JSON
// containing the user's information (e-mail address, etc.)
// Send the request
isc.RPCManager.sendRequest({
actionURL: "/myapp/php/login.php",
httpMethod: "GET",
contentType: "text/xml",
useSimpleHttp:true,
serverOutputAsString:true,
params: {
user: theCreds.username,
pass: theCreds.password
},
callback: "checkLogin(rpcResponse, data, rpcRequest)",
theDialogCallback: theFunction
});
}
function checkLogin(response, data, request) {
// Set up our authentication flag
var myObject = eval('(' + data + ')');
var authFlag = myObject.response.authorized;
// Check if returned user id is defined
if (authFlag == '1') {
// Flag to tell the login dialog we're cool; you can close now
authFlag = true;
// Launch our application
mainWindow.show();
var user_name = myObject.response.data[0].user_name;
isc.say(user_name);
} else {
// Tell the login box the user login failed
authFlag = false;
}
// We stored the original callback function in the request object
request.theDialogCallback(authFlag);
}
Try setting evalResult to true and not setting serverOutputAsString. If that doesn’t seem to work, use echoAll() to inspect rpcResponse.data.