When I post data to my local server it gives error

Viewed 30

I am using server on express and posting data from html file with post. And when I post it gives this error:

Access to fetch at 'http://192.168.0.112:8000/pc' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

here is index.html:

<body>
  <h1 id="Text">Welcome</h1>
  <button id="copy">Copy to iphone</button>
  <script>
    document.getElementById('copy').addEventListener('click', function () {
      let text = navigator.clipboard.readText()
      const options = {
        method: 'POST',
        body: JSON.stringify(text),
        headers: {
          'Content-Type': 'application/json'
        }
      }
      fetch('http://192.168.0.112:8000/pc', options)
    })
  </script>
</body>

And this is server.js:

const express = require('express');
const preserver = express();
const http = require('http');
const server = http.createServer(preserver);

preserver.post('/pc', (req, res) => {
  var msg = req.body.clip
  console.log(msg)
})

server.listen(8000, '192.168.0.112', () => {
})
0 Answers
Related