-1
/*
* @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?

8
  • The error is in your http.js script. Post it including the referenced line 29, and as text, not as image. Commented Jan 17, 2020 at 11:25
  • 1
    Please go read How to Ask. Code relevant to your problem belongs directly into your question, in text form and properly formatted. DO NOT just show images of code. Commented Jan 17, 2020 at 11:25
  • Does this answer your question? JavaScript error (Uncaught SyntaxError: Unexpected end of input) Commented Jan 17, 2020 at 11:32
  • Please add the entire script to your question. Commented Jan 17, 2020 at 12:00
  • php script or js? Commented Jan 17, 2020 at 12:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.