php 格式异常:意外字符(位于字符1处).../

bq9c1y66  于 2023-04-04  发布在  PHP
关注(0)|答案(1)|浏览(108)
Future<void> insert()async{
   try{
     if( _formKey.currentState!.validate()){
       String Url="http://10.0.2.2/class/insert.php";
       var res=await http.post(Uri.parse(Url),body: {
         "title":_titlecontroller.text.toString(),
         "desc":_descriptioncontroller.text.toString(),
         "desc1":_description1controller.text.toString(),
         "desc2":_description2controller.text.toString(),
         "desc3":_description3controller.text.toString(),
         "code":_codecontroller.text.toString(),
         "code1":_code1controller.text.toString(),
         "code2":_code2controller.text.toString(),
       });
       var response=jsonDecode(res.body);

       if (response["success"]==true) {
         print("success");
       } else {
         print("some issue");
       }
     }
     else{
       Utils.showErrorMessage("Enter all fields", context);
     }
   }
   catch(e){
     Utils.showErrorMessage("$e", context);
     print(e);
   }

  }

当我调用这个函数时,发生了这个错误

I/flutter (13861): FormatException: Unexpected character (at character 1)
I/flutter (13861): <br />
I/flutter (13861): ^
4si2a6ki

4si2a6ki1#

在评论中,我告诉你,请先检查你的端点一次 Postman 或其他人,看看你是否有问题连接到服务器或没有.你可以更新你的代码,以确保你有问题与你的代码或没有:

String Url="http://10.0.2.2/class/insert.php";
var headers = <String, String>{'Content-Type': 'application/json'};
var res = await http.post(Uri.parse(Url), headers: headers, body:{...});

if (res.statusCode == 200) {
  if (res.headers['content-type'] == 'application/json') {
    var response = jsonDecode(res.body);
    if (response["success"] == true) {
      print("success");
    } else {
      print("some issue");
    }
  } else {
    throw FormatException('Invalid content-type: ${res.headers['content-type']}');
  }
} else {
  throw Exception('Failed to load data');
}

告诉我发生了什么
快乐编码...

相关问题