CORS“ SyntaxError:在at处输入意外结束,使用fetch API vs PHP

问题描述 投票:-2回答: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 serverjs代码中带有双星号的行是行错误。

<?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'
]);

即使我认为这是不必要的,我也包括了整个获取API类。我想我提供了所有需要的细节。我确保代码可以在其他服务器上运行,但只有在我的服务器上才出问题。我在这里做错了什么?我该怎么办才能解决问题?

javascript php http-headers apache2 fetch
1个回答
0
投票

header('Access-Control-Allow-Headers: X-PINGOTHER, Content-Type');实际上,值得进行一些研究。魔术线帮助我解决了同样的问题。

© www.soinside.com 2019 - 2024. All rights reserved.