【问题标题】:Failed to post data using this.http.post using Angular from client's side无法从客户端使用 Angular 使用 this.http.post 发布数据
【发布时间】:2018-01-22 01:53:57
【问题描述】:

我正在尝试在客户端使用 Angular 将数据推送到 MySQL 数据库,并在服务器端表达 JS。当我使用 Postman 测试它时,服务器端的 post 功能端已经工作了。代码如下所示:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));
app.post('/newPush', function(req, res) {
  var data = JSON.stringify(req.body);
  var dt = JSON.parse(data);
  let sql = 'INSERT INTO results(question1, question2, question3, question4)values("' + dt.question1 + '", "' + dt.question2 + '", "' + dt.question3 + '", "' + dt.question4 + '")';
  let query = db.query(sql, (err, result) => {
    if (err) {
      throw err;
    }
    console.log(result);

    res.send(JSON.stringify(req.body) + 'success');
  })
})

我已经尝试使用邮递员发布一些数据并且它有效。我使用邮递员发布的成功发布到 MySQL 数据库。然后我尝试使用 Angular Typescript 从客户端发布数据,但看起来我做错了。数据根本没有出现在我的数据库中。我还没弄清楚是什么让它不起作用。以下是使用 Angular 的客户端代码的样子:

//component.ts

newForm: FormGroup;

constructor(private http: Http, public builder: FormBuilder) {
  this.newForm = this.builder.group({
    question1: [null],
    question2: [null],
    question3: [null],
    question4: [null],
  });
}

pushNewResults() {
  console.log('1', this.newForm.controls.question1.value);
  console.log('2', this.newForm.controls.question2.value);
  console.log('3', this.newForm.controls.question3.value);
  console.log('4', this.newForm.controls.question4.value);

  return this.http.post(
    'http://localhost:8000/newPush', {
      question1: this.newForm.controls.question1.value,
      question2: this.newForm.controls.question2.value,
      question3: this.newForm.controls.question3.value,
      question4: this.newForm.controls.question4.value
    });
}
<div *ngIf="isVerified" align="left" class="container">
  <form [formGroup]="newForm">
    <div *ngFor="let items of jsonData">
      <div *ngFor="let items2 of items.question">
        <label>{{items2.questionid}}. {{items2.questiondesc}}</label>
        <div *ngFor="let items3 of items2.alloptions; let idx=index">
          <div class="radio">
            <input type="radio" name="question{{items2.questionid}}" [value]="items3.answer" formControlName="question{{items2.questionid}}"><b>{{items3.options}}</b>. {{items3.answer}}
          </div>
        </div><br>
      </div>
    </div>
    <div align="center">
      <button type="submit" class="btn btn-sm btn-success" (click)="pushNewResults(newForm)">SUBMIT</button>
    </div>
  </form>
</div>

question1question2question3question4 的值在提交时使用 console.log 出现,但没有进入 MySQL 数据库。谁能帮我弄清楚,好吗?请让我知道需要更多的 sn-ps。

【问题讨论】:

  • 你需要在你的http.post()之后subscribe()
  • 你的代码有安全问题,你不应该在客户端构造SQL查询!
  • @Chiu 他没有。那是他的快递代码。

标签: javascript html angular typescript express


【解决方案1】:

@CozyAzure 从评论部分回答,我只需要在我的http.post() 之后添加subscribe()

【讨论】:

    猜你喜欢
    • 2012-06-24
    • 2019-06-17
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    相关资源
    最近更新 更多