Displaying a value retrieved from an HTML request

I’m new to JS, and am sure this must be pretty basic, but I can’t seem to get it to work.

I am using a little bit of JS to retrieve a value from a web server into an app.

The JS looks like:

function done(response) {
console.log(response);
var json = JSON.parse(response);
console.log(json);
console.log(json.quote);
}

Request.get("http://www.mysite.com/mypage.php")
.then(done)
.catch(function(error) {
Quote.string = "hello world";
});

I have a text box on that screen that I want to display the retrieved string when a button is tapped.

With the code above using “hello world” it correctly displays “hello world”, so the button tap is working.

And I can see in the console log the correct retrieved value.

But I can’t get the retrieved value to appear in my Quote.string test box.

I have tried using:

var json
json
json.quote
response
(response)

Presumably if the value is getting retrieved it must be possible to call it to appear in the Quote.string?

Thanks.

Request and Quote aren’t part of normal JavaScript - are they part of some other kind of library or framework, like PencilCase, that you’re trying to work with?

The place where Quote.string currently is occurs when an error happens.
If you want a successfully retrieved response to be shown, you would have to do that from the done() function.

Oh look, there’s a 60 second video detailing exactly how to do it at the PencilCase HTTP request page too.

Sorry Paul, I’m an idiot. I am using Pencil case, and had been looking at that tutorial. The mistake I made was that my page on the web server had a bit of HTML, rather than literally just the required text. Plus I was trying to get it to fire a couple of triggers, so it looked like it was’t working, and I went a bit round the houses with it.

So that is working now, but I still can’t get the triggers to work. Its for a login, so am trying to get it to go to one card if the returned string = ‘success’ and another if it isn’t.

So I have the two behaviours added, and the JS added to the expression above:

if (json.quote === "Success") {
Event.trigger("loginSuccess");
} else {
Event.trigger("loginFail");
}

Does it need to be in a particular point in the code, or with anything else around it?

Yes, it needs to be in that done function.

If you want to use json.quote, then the done function will need to have its parameter called json

For example:

function done(json) {
    if (json.quote === "Success") {
        ...
    }
}

Thanks Paul - that’s working now.

Sorry - just a follow up on this - my issue now is with determining what happens if someone is or isn’t successful logging in.

On the web server I have some code to check this, and have put the string inside an IF clause:

<?php if (WA_Auth_RulePasses("Logged in to trade_users")){ ?>{"quote":"Log in Success"}<?php } ?>

But the app is still retrieving that string regardless.

Not sure if the solution lies in the JS or the PHP, but in layman’s terms I just need a string to passed back to the app / PC if that rule passes.

If the app is still receiving the quote, then is seems that the if condition is being considered to be true.

Thanks Paul - that’s probably good, if a little confusing right now.

Although having said that it seems to be failing again.

I have the PHP code on the web server that should check the log in, and if I just have that plus:

{"login":"Log in Success"}

It works as expected.

But once I put that in the IF:

<?php if (WA_Auth_RulePasses("Logged in to trade_users")){ ?>{"quote":"Log in Success"}<?php } ?>

It failed.

But I can see that if I change that to:

<?php if (WA_Auth_RulePasses("Logged in to trade_users")){ ?>{"quote":"Log in Success"}<?php } ?>
{"quote":"Log in Fail"}

It works - at least if it looks as though the log in has been unsuccessful.

So would it be that it was failing with:

<?php if (WA_Auth_RulePasses("Logged in to trade_users")){ ?>{"quote":"Log in Success"}<?php } ?>

Because nothing was retrieved? Which would make some sense I guess.

If all of that sounds about right then the remaining issue is with the actual log in.

On the app side I have:

function done(json) {
Quote.string = json.quote;
}

var Log_In_group_Username = UsernameInput
var Log_In_group_Password = PasswordInput
var result = Request.get('http://www.mysite.com/mypage.php', { Log_In_group_Username: Log_In_group_Username, Log_In_group_Password: Log_In_group_Password })
  .then(JSON.parse)
  .then(done)
  .catch(function(error) {
    Quote.string = "Nuts! Something went wrong."; 
});

And on the web server I have this, which is what should be grabbing the username and password from the app, just as it would from a form on the website, and then go off and do its log in check.:

"tableName" => "trade_users",
"columns" => explode($WA_Auth_Separator,"Username".$WA_Auth_Separator."Password"),
"columnValues" => explode($WA_Auth_Separator,"".((isset($_POST["Log_In_group_Username"]))?$_POST["Log_In_group_Username"]:"")  ."".$WA_Auth_Separator."".((isset($_POST["Log_In_group_Password"]))?$_POST["Log_In_group_Password"]:"")  .""),

It took a while, but I eventually got this working.

For the benefit of others that may be having the same problem, would you mind sharing how your solution varies from the code that you posted, along with any other pertinent info?

The code ended up like this:

function done(json) {
  Quote.string = json.quote;
  if (json.quote === 'Log in Success') {
  Event.trigger('loginSuccess');
  } else {
  Event.trigger('loginFail');
  }
}

var Log_In_group_Username = UsernameInput
var Log_In_group_Password = PasswordInput
Request.post('http://www.mysite/mypage.php', { Log_In_group_Username: Log_In_group_Username, Log_In_group_Password: Log_In_group_Password })
  .then(JSON.parse)
  .then(done)
  .catch(function(error) {
    Quote.string = "Log In Failed - Please try again."; 
});

One thing worth noting was making sure I changed it to Request.post on the app side to match the code on the web server.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.