Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught (in promise) SyntaxError: Unexpected end of input

I don't seem to find any missed end input. This is the error that I recieve:

Uncaught (in promise) SyntaxError: Unexpected end of input at fetch.then.response (InventoryOnHand.js:33)

Below is my code: I have a value for the url.

 fetch(url + "GetItemMasterList", { 'mode': 'no-cors' })
   .then(response => response.json())
   .then(function (data) {
     console.log(data)
 });
like image 401
JEROMI Avatar asked Sep 16 '25 11:09

JEROMI


1 Answers

This is a well known problem with CORS policy. To overcome this problem you need access rights to the server side API. In particular, you have to add a line in the header of php or another server endpoint:

<?php
header('Access-Control-Allow-Origin: *');
//or
header('Access-Control-Allow-Origin: http://example.com');

// Reading JSON POST using PHP
$json = file_get_contents('php://input');
$jsonObj = json_decode($json);

// Use $jsonObj
print_r($jsonObj->message);

...
// End php
?>

Also, make sure NOT to have in the header of your server endpoint:

header("Access-Control-Allow-Credentials" : true);

Model of working fetch code with POST request is:

const data = {
        optPost: 'myAPI',
        message: 'We make a research of fetch'
    };
const endpoint = 'http://example.com/php/phpGetPost.php';

fetch(endpoint, {
    method: 'POST',
    body: JSON.stringify(data)
})
.then((resp) => resp.json())
.then(function(response) {
    console.info('fetch()', response);
    return response;
});
like image 81
Roman Avatar answered Sep 18 '25 10:09

Roman