【问题标题】:Not able to fetch data from my own api in flutter无法从我自己的 api 中获取数据
【发布时间】:2022-01-21 22:59:42
【问题描述】:

我无法从我自己的 api 中获取数据,但是是的,我可以从虚假 api 中获取数据,例如http://jsonplaceholder.typicode.com/posts/

我的代码

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: RequestPage(),
    );
  }
}

class RequestPage extends StatefulWidget {
  const RequestPage({Key? key}) : super(key: key);

  @override
  State<RequestPage> createState() => _RequestPageState();
}

class _RequestPageState extends State<RequestPage> {
  Future<void> getdata() async {
    var res = await http
        .get(Uri.parse("https://myurl.com/api/"));
    print(res.body);
  }

  late String username;
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              RaisedButton(
                onPressed: getdata,
                child: Text("Make A Request"),
              ),
              Text("Response")
            ],
          ),
        ),
      ),
    );
  }
}

我的 api

<?php


header("Access-Control_Allow_Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Content-type:application/json;charset=utf-8"); 
header("Access-Control-Allow-Methods: GET");

$conn  = mysqli_connect("localhost", "root", "", "api");
$sql = "SELECT * FROM login;";
$result = mysqli_query($conn,$sql);
header('Content-Type:application/json');
if(mysqli_num_rows($result)>0){
    while($row=mysqli_fetch_assoc($result)){
        $arr[] = $row;
    }
    echo json_encode($arr);
}

?>

我想要的输出

[
{
id: "1",
username: "admin",
password: "123456",
},
]

我遇到的错误

错误:XMLHttpRequest 错误。 dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 906:28 获取当前 包/http/src/browser_client.dart 69:22 dart-sdk/lib/async/zone.dart 1687:54 runUnary dart-sdk/lib/async/future_impl.dart 160:18 handleValue dart-sdk/lib/async/future_impl.dart 767:44 handleValueCallback dart-sdk/lib/async/future_impl.dart 796:13 _propagateToListeners dart-sdk/lib/async/future_impl.dart 593:7 [_complete] dart-sdk/lib/async/stream_pipe.dart 61:11 _cancelAndValue dart-sdk/lib/async/stream.dart 1288:7 dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14 _checkAndCall dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39 dcall dart-sdk/lib/html/dart2js/html_dart2js.dart 37254:58 在 Object.createErrorWithStack (http://localhost:56929/dart_sdk.js:5076:12) 在 Object._rethrow (http://localhost:56929/dart_sdk.js:40477:16) 在 async._AsyncCallbackEntry.new.callback (http://localhost:56929/dart_sdk.js:40473:13) 在 Object._microtaskLoop (http://localhost:56929/dart_sdk.js:40330:13) 在 _startMicrotaskLoop (http://localhost:56929/dart_sdk.js:40336:13) 在 http://localhost:56929/dart_sdk.js:35811:9

请帮帮我

【问题讨论】:

  • 这看起来更像是一个 php 问题。您是否检查过(例如使用浏览器,因为它是一个简单的 GET 调用)您的输出实际上是什么样的?
  • 这是一个 CORS 错误,由您的标题中的拼写错误引起:header("Access-Control_Allow_Origin: *");

标签: json flutter api http call


【解决方案1】:

那个颤动的代码看起来不错。

就像@Richard 指出的header("Access-Control_Allow_Origin: *") 应该是header("Access-Control-Allow-Origin: *");

还要仔细检查您是否将“https://myurl.com/api/”替换为正确的网址。

【讨论】:

猜你喜欢
  • 2014-03-24
  • 2021-02-06
  • 2022-01-14
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多