【问题标题】:post request work in post man but not in flutter? [duplicate]在邮递员中发布请求工作但不颤抖? [复制]
【发布时间】:2023-02-10 02:28:06
【问题描述】:

我使用纯 php 创建了简单的 api,并将项目上传到 000webhost,Get 请求运行良好,但是当尝试使用 POST 请求将数据添加到数据库时,它返回内部服务器错误,即使 api 在 Postman 中运行良好。 在我添加这个之后,作为 Eboo 爵士的回答:

`<?php
// see errors:
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);`

内部服务器错误消失但仍未将数据添加到数据库中!!! 我已经尝试检查有关此问题的所有问题,但仍然找不到解决方案。 是因为 000webhost 阻止了我的发帖请求吗?

颤振中的函数:

@override
  Future<void> addStudent(String name, String grade, String note) async {
    final url = Uri.parse(
        "https://mystudentsrating.000webhostapp.com/crud/add_student.php");

    final http.Response response = await http.post(url,
        headers: {
          'Content-Type': 'application/json;charset=UTF-8',
          'Charset': 'utf-8',
          "Accept": "application/json",
          "contentType": "application/json"
        },
        body: jsonEncode(
          <String, dynamic>{
            'studentname': name,
            'studentgrade': grade,
            'studentnote': note
          },
        ));

    final result = utf8.decode(response.bodyBytes);
    final message = jsonDecode(result);

    if (response.statusCode != 200) throw Exception(message['status']);
  }
}

添加学生文件:

    <?php
include '../connection.php';

// see errors:
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

$studentName = filter_request('studentname');
$studentGrade = filter_request('studentgrade');
$studentNote = filter_request('studentnote');
// $studentstars = filter_request('studentstars'); 

$stmt = $con->prepare(
    "INSERT INTO `students` (`student_name`, `student_grade`,`student_total_grades`, `student_note`, `student_stars`) 
    VALUES ( ?,?,?,?,?)");

$stmt->execute(array(
    $studentName, $studentGrade,$studentGrade, $studentNote, 0
));

$count = $stmt->rowCount();
if ($count > 0)
echo json_encode(
    array("status" => "Success"));
    else echo json_encode(
        array("status" => "Fail"));

希望你能帮我..

【问题讨论】:

  • 500 错误是一种通用错误消息,几乎涵盖了 PHP 脚本可能出错的每一件事。检查您的服务器错误日志以找出确切的错误消息。
  • @aynber 感谢您的回复,但我如何检查服务器错误日志?如果在 flutter 调试模式下,它只显示“内部服务器错误”,这就是我如此困惑的原因:(
  • 服务器错误日志通常可以在 Web 服务器错误日志文件中找到。在 linux 系统中,它位于 /var/log 内的文件夹中,通常命名为 apacheapache2httpdnginx。有时在您的项目目录中的某处有一个error_log,这取决于您的网络服务器的设置方式。
  • @aynber 仍在尝试获取它,但我认为我不擅长 php,你能帮我更多吗?我正在使用 Windows

标签: php flutter dart


【解决方案1】:

首先你可以尝试添加用于调试目的:

`<?php
// see errors:
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);`

也许关闭你的文件 "?&gt; "

【讨论】:

  • 内部服务器错误消失了,我觉得这很好吧?但仍然没有将数据添加到数据库
  • 我得到这个:“无法访问没有内容类型“application/x-www-form-urlencoded”的请求的正文字段。#0 Request.bodyFields(包:http/src/request.dart:121:7)
猜你喜欢
  • 2019-08-25
  • 2016-10-23
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 2018-11-07
  • 2019-05-27
  • 2020-05-18
  • 2019-11-03
相关资源
最近更新 更多