fetch() unexpected end of input

前端 未结 5 2269
暖寄归人
暖寄归人 2020-11-30 02:34

I am using fetch() to grab data from api server. My error looks like this:

Uncaught (in promise) SyntaxError: Unexpected end of input at 
  fetch.then.blob.
         


        
5条回答
  •  悲哀的现实
    2020-11-30 03:02

    You met with the CORS origin policy problem. To tackle this you need rights to access the server side API. In particular, you need to add a line in the header of php or another server endpoint:

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

提交回复
热议问题