000webhost api返回net :: ERR_EMPTY_RESPONSE

时间:2018-11-05 10:30:14

标签: php reactjs api fetch

我用php制作了一个api,问题是当我尝试使用fetch来检索数据时不起作用OPTIONS http://my-app.000webhostapp.com/newapi/ net::ERR_EMPTY_RESPONSE的问题是,当我尝试向邮递员发出请求时一切正常,任何提示吗? 返回的示例代码:

public function throwError($code, $message) {
            header('Access-Control-Allow-Origin: *');
            header("Access-Control-Allow-Methods: POST");
            header("Access-Control-Allow-Headers: Content-Type, Authorization, Access-Control-Allow-Headers, X-Requested-With");
            header("content-type: application/json; Charset='UTF-8'");
            header("cache-control: no-cache;");
            $errorMsg = json_encode(['error' => ['status'=>$code, 'message'=>$message]]);
            echo $errorMsg;
            exit();
        }

如果我尝试调用api throw https,我什至得到net::ERR_SPDY_PROTOCOL_ERROR

使用提取的示例调用:

fetch("http://my-app.000webhostapp.com/newapi/", {
      method: "post",
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json"
      },
      body: JSON.stringify({
        name: "getAllSeriesByType",
        param: {
          type: serieType
        }
      })
    })
      .then(response => {
        if (response.ok) {
          return response.json();
        } else {
          throw new Error("Something went wrong");
        }
      })
      .then(responseData => {
        if (responseData.response.status === 200) {
          this.setState({
            series: responseData.response.result,
            loading: false
          });
        }
      })
      .catch(error => {
        this.setState({
          series: "There was an internal error retry later."
        });
        console.error(error);
      });

1 个答案:

答案 0 :(得分:0)

问题在于000webhosting他们遇到了一些问题:(他们花了3个星期来解决它。