【问题标题】:Why php can not read encode values send from flutter?为什么 php 无法读取从颤振发送的编码值?
【发布时间】:2021-02-20 02:05:13
【问题描述】:

我试图通过 php 中的不同帖子类型获取值

$data['data'] = 'request';
$data['_REQUEST'] = $_REQUEST;
$data['_POST'] = $_POST;
$data['_SERVER'] = $_SERVER;
header('Content-Type: application/json');
echo json_encode($data);

这是完美的,即使现在在邮递员工作。

在颤振中,我正在尝试遵循脚本,在该脚本中,我编码了正文值以与标头一起发送。

Map<String, String> headers = {
      "Content-type": "application/json; charset=UTF-8",
      "Accept": "*/*",
      "x-auth": auth_token
    };
    
    final json = jsonEncode({
      "add_view_posty": 'yes',
      "post_id":'1'
    });
    http.Response response = await http.post(url, headers: headers, body: json);

但是 php _REQUEST_POST 帖子类型不会读取上述脚本发送的值。对于上述脚本,我无法访问response.body,但response.status 显示200 响应。

以下 http 请求运行良好,但没有标头。

final json = {
      "add_view_posty": 'yes',
      "post_id":'1'
    };
    var data = await http.post(url, body: json);

有人可以帮忙吗,我很感激。谢谢

【问题讨论】:

    标签: php flutter dart


    【解决方案1】:

    你可以试试这个

    final body = {
      'name': 'Bob',
      'age': '87',
    };
    final jsonString = json.encode(body);
    final uri = Uri.http('www.example.com', '/path');
    final headers = {HttpHeaders.contentTypeHeader: 'application/json'};
    final response = await http.post(uri, headers: headers, body: jsonString);
    

    如果它不适合您,您能否与我们分享您如何在邮递员中提出请求

    【讨论】:

    • 并且还共享通过网络发送的实际数据。您是否使用 Postman 测试过您的 PHP 应用程序?
    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 2021-01-07
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多