samedi 4 septembre 2021

flutter webapp "Access-Control-Allow-Origin": "*" not working

This could look like a already posted question, but I have been looking for 2 days now for answers and could not find one.

I use the following code to send a get request

    Future<String> getData() async { 
      //final url = Uri.parse('https://reqres.in/api/users?page=2');
      final url = Uri.parse('https://127.0.0.1:80/db1.php');

      http.Response response = await http.get(
        url,
        headers: {
        "Access-Control-Allow-Origin": "*"
        });


        if (response.statusCode == 200) {
         print(response.statusCode);
          print(response.statusCode);
          print(response.body);

        } else {
        //If the server did not return a 200 OK response,
          print('no connection');
          // then throw an exception.
          throw Exception('Failed to load album');
        }

      }

And my php, just to say I do not know php at all and it could be where my problem is

    <?php

    header("Access-Control-Allow-Headers: Access-Control-Allow-Origin, Accept");
    $dsn = 'mysql:dbname=em;host=localhost';
    $dbUser = 'root';
    $password = '';

    $db = new PDO($dsn,$dbUser,$password);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


    $query = "SELECT * FROM tbl_pi_logging_keys";

    $stmt = $db->prepare($query);
    $stmt->execute();

    $userData = array();

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

      $userData['AllUsers'][] = $row;
     }

     echo json_encode($userData);
    ?>

When I run the code I get the following problem

Error: XMLHttpRequest error. C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 909:28 get current packages/http/src/browser_client.dart 71:22 C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1613:54 runUnary C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 155:18 handleValue C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 707:44 handleValueCallback C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 736:13 _propagateToListeners C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 533:7 [_complete] C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream_pipe.dart 61:11 _cancelAndValue C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream.dart 1219:7 C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 324:14 _checkAndCall C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 329:39 dcall C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/html/dart2js/html_dart2js.dart 37307:58

at Object.createErrorWithStack (http://localhost:1055/dart_sdk.js:5362:12)
at Object._rethrow (http://localhost:1055/dart_sdk.js:39548:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:1055/dart_sdk.js:39542:13)
at Object._microtaskLoop (http://localhost:1055/dart_sdk.js:39374:13)
at _startMicrotaskLoop (http://localhost:1055/dart_sdk.js:39380:13)
at http://localhost:1055/dart_sdk.js:34887:9

So can anyone tell me if they can see a mistake I am making. For the answers I looked at all include the header with the access-controll




Aucun commentaire:

Enregistrer un commentaire