Handle response - SyntaxError: Unexpected end of input when using mode: 'no-cors'

前端 未结 5 1867
南旧
南旧 2020-11-22 07:19

I tried a ReactJS fetch call to a REST-API and want to handle the response. The call works, i get a response, which i can see in Chrome Dev Tools:

function g         


        
5条回答
  •  深忆病人
    2020-11-22 07:40

    You can avoid the problem with CORS policy by adding in the header of php or another server endpoint the row:

    message);
    
    ...
    // End php
    ?>
    

    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;
    });
    

提交回复
热议问题