1

I'm working on an OAuth2 Discord login to my game's index. I try to pull nickname to my client-side and my Discord bot directly change logged user's nickname to his game name. I have nickname on my PHP-based server-side with JSON data but I don't know how can I pull it to my client-side

Example PHP Code


$myobj->username = "Test";

$myJSON = json_encode($myobj);
echo $myJSON;```

And my Javascript code:

const express = require('express');
const Discord = require('discord.js');
const client = new Discord.Client();
const app = express();
const passport = require("passport");
const { Strategy } = require("passport-discord");
const session = require("express-session");

app.use(passport.initialize());
app.use(passport.session());

passport.serializeUser(async(user, done) => {
    await (await client.guilds.cache.get("guildID").members.fetch(user.id)).roles.add("roleID")
    await (await client.guilds.cache.get("guildID").members.fetch(user.id)).setNickname(PHP DATA TO HERE)
    return done(null, user)
  });

2 Answers 2

1

You can do it via AJAX as Lajos suggested, also you can assign it to the JS variable from PHP at the moment of page rendering. All depends on your needs, so just choose the solution which is better for your case.

In your current code instead of echoing JSON, assign it to the JS variable.

echo "<script>let myJsonInJs = $myJson</script>";

so later you can use that variable somehow in your JS, i.e.:

<script>
   console.log(myJsonInJs)
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Assuming that the PHP in question runs before the page is sent to the browser initially this could be a solution (upvoted). If he page is already loaded, then AJAX is needed.
1

You will need to send an AJAX (Asynchronous Javascript And XML) request to your server-side. Let's implement a function for this purpose:

function sendRequest(type, url, callback, async, params) {
    if (async !== false) async = true;
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = callback;
    xhttp.open(type, url, async);
    xhttp.setRequestHeader(\"Content-type\", \"application/x-www-form-urlencoded\");
    xhttp.send(params);
}

You can call it like:

sendRequest("POST", "yoururl", function() {
    if (this.readyState === 4) {
        console.log(JSON.parse(this.responseText));
    }
}, true, "username=Test");

Change yoururl to the proper location of your PHP script and pass some parameters if needed.

EDIT

If you are inside the NodeJS environment and you are able to send the request at this level, then you can use fetch, as Endless has pointed out in the comment section.

2 Comments

Or simply put: fetch('yoururl', { method: 'POST', body: new URLSearchParams({username: 'Test'}) }).then(res => res.json()).then(callback)
@Endless edited my answer to reflect your point.

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.