I want to use fetch() in javascript

Asked 1 years ago, Updated 1 years ago, 30 views

Thank you for your cooperation.

To get Googlehome to talk from your browser
https://qiita.com/kyota/items/453047f236ca5488027c
Based on , I created index.html as shown below.

<html>
<body>
    <script>

        function fetch('./', {
            method: 'POST',
            cache: 'no-cache',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({msg:'})
            })
            .then(res=>res.json())
            .catch(error=>console.error('Error:', error))
            .then(response=>console.log('Success:', response));
    </script>

    <p>
        <input type="button" value="Good morning" onclick="fetch()">
    </p>
    <form>
        <input type="text" name="speak1" onclick="fetch()">
        <button>Talk</button>
    </form>
</body>
</html>

If you press the button, Googlehome will say "Good morning" or the word entered in the field
I wanted to speak, but it didn't work out as I expected.

I would appreciate it if you could tell me how to use fetch.

javascript html

2022-09-30 17:21

1 Answers

function is not required.

Using Functions

You seem to be confusing the function's declaration (definition) and invocation.Now that you want to use the function fetch that is already included in your browser environment, it's not a declaration, it's a call. Instead of function, it's fetch(argument...).

Note: Function - JavaScript|MDN

How to Determine Errors

If you are experiencing problems in general, you should check (or not) what errors are occurring.This can be used in different browsers, but there is a "console" for "development tools."In this example, you should be able to see an error that means "syntax error."

Note: Web Console - Development Tools | MDN


2022-09-30 17:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.