Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Post to local API

dannlea

New Coder
Hey everyone,

I'm working with a desktop music player that has an API allowing for remote control of the player.
I'm trying to create a website that has basic controls for the player... pause, play, skip, and maybe a button for full volume / background music volume.

The API documentation calls for the following, for example:

Code:
POST http://localhost:9863/query

"command": String,
"value": String (optional)

track-play - Play music

I've tried the following code, but I'm getting no response and the console isn't logging anything either:

Code:
<html>
<script>
    document.getElementById("play").addEventListener('click', async _ => {
        try {     
            const response = await fetch('http://localhost:9863/query', {
            method: 'post',
            command: 'track-play'
            });
            console.log('Completed!', response);
        } catch(err) {
            console.error(`Error: ${err}`);
        }
    });
</script>
<body>
    <p>Click the button play</p>
    <button id="play">play </button>
</body>

</html>

Sorry if this is super simple, I've been out of JS for a super long time and I don't really remember how to work with it :)
 
Hey everyone,

I'm working with a desktop music player that has an API allowing for remote control of the player.
I'm trying to create a website that has basic controls for the player... pause, play, skip, and maybe a button for full volume / background music volume.

The API documentation calls for the following, for example:

Code:
POST http://localhost:9863/query

"command": String,
"value": String (optional)

track-play - Play music

I've tried the following code, but I'm getting no response and the console isn't logging anything either:

Code:
<html>
<script>
    document.getElementById("play").addEventListener('click', async _ => {
        try {   
            const response = await fetch('http://localhost:9863/query', {
            method: 'post',
            command: 'track-play'
            });
            console.log('Completed!', response);
        } catch(err) {
            console.error(`Error: ${err}`);
        }
    });
</script>
<body>
    <p>Click the button play</p>
    <button id="play">play </button>
</body>

</html>

Sorry if this is super simple, I've been out of JS for a super long time and I don't really remember how to work with it :)
Hi there,
You might just be missing a call to .then() . You aren't getting anything back from your call because the function gets completed before your fetch even returns anything. Adding the call to .then, should take care of that.
JavaScript:
function doStuff(){
    fetch('http://apiurl', {
        //add your headers here
    })
    .then(response => {console.log(response)})
}


Also, I do believe someone may have pointed out a few errors you may have missed in your prior post
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom