-2
/*
* @version: 1.0.0
* @author: Murod Parmonov
* @licence: MIT
*/

class HTTP{


    // post request
    post(url, data){
        return new Promise((resolve, reject) => {
                fetch(url, {
                    method: 'POST',
                    headers: {
                        'Content-type': 'application/json'
                    },
                    body: JSON.stringify(data),
                    'mode': 'no-cors'
                }).
                **then(response => response.json()).then(data => resolve(data)).
                catch(err => reject(err));**
            }
        );
    }



}

const ht = new HTTP;  // http  fetch API class
ht.post('https://correctserverurl.com/', data).then(resData => console.log(resData))    .catch(err => console.log(err));

error I got from the server the noted with double stars line in js code is the line error.

<?php
header('Access-Control-Allow-Origin: correctdomain.com');
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Headers:  X-Requested-With");

$ping = file_get_contents("php://input");
file_put_contents('ping.json', $ping);
$ping = json_decode($ping, 1);
echo json_encode($data = [
    'status' => 'success'
]);

I included whole class of fetch API even I think it is unneccessary. I think I provide every detail needed. I ensured that the code works on others server but only on my server something is wrong. what am I doing wrong here? what should I do to solve the problem?

1 Answers1

0

header('Access-Control-Allow-Headers: X-PINGOTHER, Content-Type'); actually, this was worth to do a little bit research. the magic line helped me to solve the the same problem.

Murod
  • 14
  • 1
  • 5