-4

i'm trying to send few form details to the backend API using angular6, but it doesn't work as i expected, and how do i add Observable to this, im expecting to use observable instead of promises.

login.component.html

loginUser(e){

    var username = e.target.elements[0].value;
    var password = e.target.elements[1].value;
    console.log(username,password);

      this.user.setUserLoggedIn()
      var body = "username=" + username + "password=" + password;
      this.http.post("http://localhost:8080/user/all", body).subscribe((data) => {});
       this.router.navigate(['fullpage'])

    return false;
  }

login.component.html

<form id="loginForm" novalidate="novalidate" (submit)="loginUser($event)">
                                    <div class="form-group">
                                        <label for="username" class="control-label">Username</label>
                                        <input type="text" class="form-control" id="username" name="username" value="" required="" title="Please enter you username" placeholder="[email protected]">
                                        <span class="help-block"></span>
                                    </div>
                                    <div class="form-group">
                                        <label for="password" class="control-label">Password</label>
                                        <input type="password" class="form-control" id="password" name="password" value="" required="" title="Please enter your password" placeholder="******">
                                        <span class="help-block"></span>
                                    </div>
                                    <div id="loginErrorMsg" class="alert alert-error hide">Wrong username or password</div>
                                    <div class="checkbox">
                                    </div>
                                    <button type="submit" class="btn btn-success btn-block">Login</button>

                                </form>

Error:

ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[LogincomponentComponent -> Http]:
StaticInjectorError(Platform: core)[LogincomponentComponent -> Http]: NullInjectorError: No provider for Http! Error: StaticInjectorError(AppModule)[LogincomponentComponent -> Http]:
StaticInjectorError(Platform: core)[LogincomponentComponent -> Http]: NullInjectorError: No provider for Http!

0

3 Answers 3

0

You should import HttpModule or HttpClientModule in app.module file and add it to imports array

If your http object is of type Http import HttpModule If your http object is of type HttpClient import HttpClientModule

and body object is not of type string it is object type it should be

body = { username, password }
Sign up to request clarification or add additional context in comments.

5 Comments

HttpModule is deprecated in angular6, don't recommend usage of deprecated modules
I added it to now i'm getting ERROR Response {_body: "{"timestamp":1529392191129,"status":415,"error":"U…harset=UTF-8' not supported","path":"/user/save"}", status: 415, ok: false, statusText: "OK", headers: Headers, …}
@bambam I did not recommend to use HttpModule just advised to import it if the already import http object is from HttpModule
@SibaBoba It is probably a Backend error
Why the down vote?
-1

You need to Add http module into your app.module like this -

import { HttpModule } from '@angular/http';

@NgModule({
  declarations: [],
  imports: [HttpModule]
...

PS:

  • If you are using HttpClientModule make changes accordingly
  • HttpModule is deprecated in v>5

9 Comments

now i'm getting ERROR Response {_body: "{"timestamp":1529392191129,"status":415,"error":"U…harset=UTF-8' not supported","path":"/user/save"}", status: 415, ok: false, statusText: "OK", headers: Headers, …}
This error is coming from backend server
HttpModule is deprecated in angular6, don't recommend usage of deprecated modules
@bambam Can you please let me know where in the question mentioned that OP is using httpClient ? also I am mentioned in my answer about httpClientModule as well
and FYI httpModule is still in working state for some versions , so why DOWNVOTE??
|
-1

In order to use Http in your app you will need to add the HttpModule to your app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule, ErrorHandler } from '@angular/core';
import { HttpModule } from '@angular/http';
...
  imports: [
    BrowserModule,
    HttpModule
  ]

2 Comments

HttpModule is deprecated in angular6, don't recommend usage of deprecated modules
now i'm getting ERROR Response {_body: "{"timestamp":1529392191129,"status":415,"error":"U…harset=UTF-8' not supported","path":"/user/save"}", status: 415, ok: false, statusText: "OK", headers: Headers, …}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.