在我的flutter应用程序中,我决定通过从本地db (xampp)在线托管我的应用程序来直播,但现在每当我运行我的应用程序时,我总是得到
FormatException: Unexpected character (at character 1)
<html><body><script type="text/javascript" src="/aes.js" ></script><script>...
//Full html code
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("3bbe2c7d2bd575630c6e4c0b1537ed79");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="https://www.quelib.com/src/get_api/get_uni.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
我在谷歌上搜索了stackoverflow和其他网站,但我得到的答案是添加标题
headers: {
'Content-Type': 'application/json',
'Charset': 'utf-8',
"Accept": 'application/json'
}
或检查我的statusCode或编码我的身体参数使用jsonEncode(),因为我一直得到Bad State
错误每当我运行我的应用程序与'Content-Type': 'application/json',
Future<List<String>> getAllUni() async {
List<String> uniAll = [];
try {
var apiUrl = url + "get_api.php";
final Map<String, dynamic> data = {"action": "GET_UNI"};
await http.post(Uri.parse(apiUrl), body: jsonEncode(data), headers: {
'Content-Type': 'application/json',
'Charset': 'utf-8',
"Accept": 'application/json'
}).then((response) {
debugPrint(response.body);
debugPrint(json.decode(response.body));
print(response.body);
print(json.decode(response.body));
if (json.decode(response.body) == 'no_data') {
} else {
List jDecode = json.decode(response.body);
if (response.statusCode == 200) {
jDecode.forEach((e) {
print(e.toString());
uniAll.add(e['name'].toString());
});
} else {}
}
});
} catch (e) {
ContentReport.report.catchReport("get_api.dart", "getAllUni",
e.toString());
}
return uniAll;
}
我的PHP代码
$action = $_POST['action'];
if($action == 'GET_UNI') {
$sql = $conn->prepare("SELECT * FROM uni ORDER BY name ASC");
$sql->execute();
$res = $sql->get_result();
if (mysqli_num_rows($res) > 0) {
while ($row = $res -> fetch_assoc()) {
$data[] = $row;
}
echo json_encode($data);
}
else {
echo json_encode("error_data");
}
}
请我如何解决这个原因的url链接工作得很好。如果你需要更多的解释,请告诉我
header('Content-type: application/json');
$action = $_POST['action'];
if($action == 'GET_UNI') {
$sql = $conn->prepare("SELECT * FROM uni ORDER BY name ASC");
$sql->execute();
$res = $sql->get_result();
if (mysqli_num_rows($res) > 0) {
while ($row = $res -> fetch_assoc()) {
$data[] = $row;
}
echo json_encode($data);
}
else {
echo json_encode("error_data");
}
}
使您的PHP代码响应更有信息量。一旦使用下面的代码:
<?php
$response["error"] = false;
$response["errmsg"] = "No message";
$response["date"] = array();
if(isset($_POST["action"])){
$action = $_POST['action'];
if($action == 'GET_UNI') {
$sql = $conn->prepare("SELECT * FROM uni ORDER BY name ASC");
$sql->execute();
$res = $sql->get_result();
if (mysqli_num_rows($res) > 0) {
while ($row = $res -> fetch_assoc()) {
array_push($response["data"], $row);
}
}else {
$response["error"] = true;
$response["errmsg"] = "NO any row found.";
}
}
}else{
$resposne["error"] = true;
$response["errmsg"] = "No action parameter submitted through POST.";
}
header('Content-Type: application/json');
echo json_encode($response);
?>
你可以看看这个例子在FlutterCampus:有效的方式生成嵌套或复杂的JSON数据使用PHP