【问题标题】:Send data to the backend API Angular 6 [duplicate]将数据发送到后端 API Angular 6 [重复]
【发布时间】:2018-11-28 01:35:13
【问题描述】:

我正在尝试使用 angular6 将一些表单详细信息发送到后端 API,但它没有按我预期的那样工作,我该如何添加 Observable,我希望使用 observable 而不是 Promise。

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="example@gmail.com">
                                        <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 错误:未捕获(承诺):错误: StaticInjectorError(AppModule)[LogincomponentComponent -> Http]:
StaticInjectorError(Platform: core)[LogincomponentComponent -> Http]: NullInjectorError:没有 Http 提供者!错误:StaticInjectorError(AppModule)[LogincomponentComponent -> Http]:
StaticInjectorError(Platform: core)[LogincomponentComponent -> Http]: NullInjectorError: 没有 Http 提供者!

【问题讨论】:

    标签: javascript angular rxjs observable angular6


    【解决方案1】:

    您应该在 app.module 文件中导入 HttpModuleHttpClientModule 并将其添加到导入数组中

    如果你的 http 对象是 Http 类型的 import HttpModule 如果你的 http 对象是 HttpClient 类型的 import HttpClientModule

    并且body对象不是字符串类型它是对象类型 应该是

    body = { username, password }
    

    【讨论】:

    • HttpModule 在 angular6 中已弃用,不推荐使用已弃用的模块
    • 我添加到现在我得到错误响应 {_body: "{"timestamp":1529392191129,"status":415,"error":"U...harset=UTF-8' not支持","path":"/user/save"}", status: 415, ok: false, statusText: "OK", headers: Headers, ...}
    • @bambam 我不建议使用 HttpModule,如果已经导入的 http 对象来自 HttpModule,建议导入它
    • @SibaBoba 可能是后端错误
    • 为什么投反对票?
    【解决方案2】:

    您需要像这样将http 模块添加到您的app.module -

    import { HttpModule } from '@angular/http';
    
    @NgModule({
      declarations: [],
      imports: [HttpModule]
    ...
    

    PS:

    • 如果您使用的是HttpClientModule,请进行相应的更改
    • HttpModule 在 v>5
    • 中已弃用

    【讨论】:

    • 现在我收到了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, …}
    • 此错误来自后端服务器
    • HttpModule 在 angular6 中已弃用,不推荐使用已弃用的模块
    • @bambam 你能告诉我问题中提到的 OP 在哪里使用 httpClient 吗?我在关于httpClientModule 的回答中也提到了我
    • 并且仅供参考 httpModule 对于某些版本仍处于工作状态,那么为什么要 DOWNVOTE??
    【解决方案3】:

    为了在您的应用中使用 Http,您需要将 HttpModule 添加到您的 app.module.ts:

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

    【讨论】:

    • HttpModule 在 angular6 中已弃用,不推荐使用已弃用的模块
    • 现在我收到错误响应 {_body: "{"timestamp":1529392191129,"status":415,"error":"U…harset=UTF-8' not supported"," path":"/user/save"}", status: 415, ok: false, statusText: "OK", headers: Headers, ...}
    猜你喜欢
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 2018-05-04
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    相关资源
    最近更新 更多