Angular4 http post方法不会将数据传递到Web API [英] Angular4 http post method does not pass data to web api

查看:46
本文介绍了Angular4 http post方法不会将数据传递到Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用http post方法时,它返回数据未正确传递".我通过正文传递了数据,还尝试使用json.stringify()进行传递,还尝试了使用以下内容类型.但这没用.

When I called the http post method it returns "data does not pass correctly".I passed data through body and I also tried by passing using json.stringify() I also tried by using content type as following also. But it didn't work.

const headers = new Headers({ 'Content-Type': 'application/json' });
const options = new RequestOptions({ headers: headers });
this.http_.post('http://localhost:12412/api/employee', remark, options)
  .subscribe((data) => {console.log(data)})};  

我在下面的component.ts文件中附加了post调用方法:

I attached the post call method in component.ts file below:

import { Component, OnInit } from '@angular/core';
import 'rxjs/add/operator/map';
import { Injectable } from '@angular/core';
import { Response, Headers, RequestOptions,Http} from '@angular/http';
import { HttpClient} from '@angular/common/http';
import { Time } from '@angular/common/src/i18n/locale_data_api';
import { NgbDateStruct } from '@ng-bootstrap/ng-bootstrap/datepicker/ngb-date-struct';
import { NgbCalendar } from '@ng-bootstrap/ng-bootstrap/datepicker/ngb-calendar';
import {Ng2OrderModule} from 'ng2-order-pipe';
import {Observable} from "rxjs/Rx";
import { PostChangesService } from './PostChanges.service';
import { Body } from '@angular/http/src/body';


@Component({
  selector: 'app-table',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.css']
})

export class TableComponent implements OnInit {
  constructor(private http: HttpClient, private http_:Http){

  }

  EditValue(event,remark,log_id) {
    console.log(remark+" "+log_id );

    //  var headers = new Headers();
    //  headers.append('Content-Type', 'application/x-www-form-urlencoded');
    //  var body="remark="+remark;
    //  //let body = JSON.stringify(remark);
    //    this.http_.post('http://localhost:12412/api/employee?',{body:remark},{headers:headers})
    // .subscribe((data) => {console.log(data)}
    // );
    const headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
    const options = new RequestOptions({ headers: headers });
    //const params = new URLSearchParams();

    // let body = JSON.stringify(remark);
   this.http_.post('http://localhost:12412/api/employee?',remark,options)
    .subscribe((data) => {console.log(data)} 
   )};  
  }
}

Web api post方法如下:

And the web api post method is as follows:

[HttpPost]
public string Post([FromBody]string remark)
{
    if (remark != null)
    {
        return remark + " _ " ;
    }
    else
        return "data doesn't pass correctly";
}

推荐答案

尝试将您的帖子数据作为这样的对象传递:

Try passing your post data as object like this:

 // let body = JSON.stringify(remark);
 this.http_.post('http://localhost:12412/api/employee',{remark:remark},options)
   .subscribe((data) => {console.log(data)} 
 )}; 

Web API发布方法

[HttpPost]
public string Post([FromBody]RemarkModel model)
{
   if (model.remark != null)
   {
                return model.remark + " _ " ;
   }
   else
   {
    return "data doesn't pass correctly";             
   }
}

public class RemarkModel
{
   public string remark {get; set;}
}

这篇关于Angular4 http post方法不会将数据传递到Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆