ClientException: XMLHttpRequest error., uri=http://localhost:3000/api/users/allUsers

147 views Asked by At

When Try to work with the api in the Desktop Simulator it is working well but with Edge/Chrome It does not work;

class UserDao {   static const baseUrl = "http://localhost:3000/api"; 
 //Get All Users   static Future<List<UserModel>?> getAllUsers() async
{
    try {
     final resp = await http.get(
        Uri.parse("$baseUrl/users/allUsers"),
        headers: {
          "Accept": "application/json",
          "Access-Control_Allow_Origin": "*",
           "Access-Control-Allow-Headers": "Access-Control-Allow-Origin, Accept"
         },
       );
      var listUser = json.decode(resp.body);
      List<UserModel> users = [];
      listUser.forEach((user) {
         UserModel u = UserModel(
             idUser: user['id_user'],
             firstName: user['firstName'],
            lastName: user['lastName'],
             email: user['lastName'],
            password: user['password']);
         users.add(u);
       });
       return users;
      } 
    catch (err) {
       print(err);
       return null;
     }   
   } 
 }
1

There are 1 answers

3
Otmane Bachri On

Now It works I cahnge just the cors (in the backend Node js Express) to {origin : "*"} to accept ant sorces , I think that it was working in the Desktop simulator because By Default it is runing in the localhost:8080 but edge and Chrome Not