猿问

使用 dart 向 php 发送 Http 请求

我尝试使用 dart 将数据发布到 php 后端。以某种方式使用 ajax,我可以获得响应,但是使用 dart,它显示 <b>Notice</b>:  Trying to get property 'email' of non-object in <b>D:\xampp\htdocs\PHPBackend\api\login\login_account.php</b> on line <b>23</b><br />

<b>Notice</b>:  Trying to get property 'password' of non-object in <b>D:\xampp\htdocs\PHPBackend\api\login\login_account.php</b> on line <b>24</b><br />


这些是我用于登录的 php 代码。


// login_account.php

$database = new Database();

$db = $database->getConnection();


$login = new Login($db);


$data = json_decode(file_get_contents("php://input"));


$login->email = $data->email;

$login->password = $data->password;


$login->loginAccount();


$login_arr = array(

  "email" => $login->email,

  "password" => $login->password

);


print_r(json_encode($login_arr));

?>

// login.php

function loginAccount(){


    // query to read single record

    $query = "SELECT

    email, password

    FROM

    " . $this->table_name . " WHERE

    email = :email AND password = :password";


    // prepare query statement

    $stmt = $this->conn->prepare( $query );


    // sanitize

    $this->email=htmlspecialchars(strip_tags($this->email));

    $this->password=htmlspecialchars(strip_tags($this->password));


    // bind id of food to be updated

    $stmt->bindParam(":email", $this->email);

    $stmt->bindParam(":password", $this->password);

    die($this->email);

    die($this->password);


    // execute query

    $stmt->execute();


    // get retrieved row

    $row = $stmt->fetch(PDO::FETCH_ASSOC);


    // set values to object properties

    $this->email = $row['email'];

    $this->password = $row['password'];

  }

}



慕桂英546537
浏览 185回答 2
2回答

MM们

附加信息有一个 dart 包为 http 请求提供了一些帮助类。Github:https : //github.com/Ephenodrom/Dart-Basic-Utils安装它:dependencies:&nbsp; basic_utils: ^1.5.1它也是 EZ-Flutter Collection 的一部分:Github:https : //github.com/Ephenodrom/EZ-Flutter 文档:https : //ez-flutter.de/docsdependencies:&nbsp; ez_flutter: ^0.2.5用法Map<String, String> headers = {&nbsp; "Some": "Header"};Map<String, String> queryParameters = {&nbsp; "Some": "Parameter"};String url = "";Map payload = "{}";&nbsp; &nbsp; Map<String, dynamic> reaponseBody;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; responseBody = await HttpUtils.postForJson(url, json. encode(payload) ,&nbsp; &nbsp; &nbsp; &nbsp; queryParameters: queryParameters, headers: headers);&nbsp; &nbsp; } catch (e) {&nbsp; &nbsp; &nbsp; &nbsp; // Handle exception, for example if response status code != 200-299&nbsp; &nbsp; }&nbsp; &nbsp; // do something with the response body附加信息 :这些都是来自 HttpUtils 类的方法。Future<Map<Response> getForFullResponse(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});Future<Map<String, dynamic>> getForJson(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});Future<String> getForString(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});Future<Map<Response> postForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});Future<Map<String, dynamic>> postForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});Future<String> postForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});Future<Response> putForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});Future<Map<String, dynamic>> putForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});Future<String> putForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});Future<Response deleteForFullResponse(String url,{Map<String, String> queryParameters,Map<String, String> headers});Future<Map<String, dynamic>> deleteForJson(String url,{Map<String, String> queryParameters,Map<String, String> headers});Future<String> deleteForString(String url,{Map<String, String> queryParameters,Map<String, String> headers});Map<String, dynamic> getQueryParameterFromUrl(String url);String addQueryParameterToUrl(String url, Map<String, dynamic> queryParameters);

幕布斯6054654

我可以在 Dart 代码中看到您正在尝试直接发送 Map 对象,而不是首先将其转换为例如 JSON。要转换为 JSON,您可以使用 dart:convert 包和以下方法:var&nbsp;encodedLoginObj&nbsp;=&nbsp;json.encode(loginObj);
随时随地看视频慕课网APP
我要回答