【问题标题】:CORS "SyntaxError: Unexpected end of input at" with fetch API vs PHPCORS“SyntaxError: Unexpected end of input at”与 fetch API vs PHP
【发布时间】:2020-01-17 11:22:49
【问题描述】:
/*
* @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 js代码中带双星号的行是行错误。

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

我包含了整个类的 fetch API,即使我认为它是不必要的。 我想我提供了所需的每一个细节。 我确保代码可以在其他服务器上运行,但仅在我的服务器上出现问题。 我在这里做错了什么? 我应该怎么做才能解决这个问题?

【问题讨论】:

  • 错误在您的 http.js 脚本中。发布它,包括引用的第 29 行,并作为text不是作为图像
  • 请阅读How to Ask。与您的问题相关的代码直接属于您的问题,以文本形式和格式正确。不要只显示代码图像
  • 请将整个脚本添加到您的问题中。
  • php脚本还是js?

标签: javascript php http-headers apache2 fetch


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-11-21
    • 2017-06-24
    • 1970-01-01
    • 2016-10-21
    • 2018-02-19
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多