【发布时间】:2021-05-10 22:24:22
【问题描述】:
我尝试从我的 Dart 应用程序向我的服务器上的 PHP 文件发送一个 POST 请求,该文件只检查是否设置了 $_POST['username'],发生的情况是请求似乎到达了服务器,但是服务器拒绝“读取”:
这是服务器在收到请求时看到的(通过使用:file_get_contents('php://input'))
{"username":"testUsername"}
但如果我尝试做类似的事情:
isset($_POST['username'])
这总是返回 false。
这是我用来发送 POST 请求的方法:
import 'dart:convert';
import 'package:http/http.dart' as http;
final String url = "my.domain.com";
final String unencodedPath = "/subfolder/myfile.php";
final Map<String, String> headers = {'Content-Type': 'application/json; charset=UTF-8'};
final Map<String,String> body = {'username': 'testUsername'};
Future<http.Response> makePostRequest(String url, String unencodedPath , Map<String, String> header, Map<String,String> requestBody) async {
final response = await http.post(
Uri.http(url,unencodedPath),
headers: header,
body: jsonEncode(requestBody),
);
print(response.statusCode);
print(response.body);
}
makePostRequest(url, unencodedPath, headers, body);
为什么 isset($_POST['username']) 看不到我的 POST 发送值?
【问题讨论】: