Angular4 http post方法不会将数据传递给web api

时间:2018-03-07 05:35:03

标签: angular typescript asp.net-web-api

当我调用http post方法时,它返回“数据未正确传递”。我通过body传递数据,我也尝试使用json.stringify()传递我也尝试使用内容类型如下。但它没有用。

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调用方法:

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方法如下:

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

3 个答案:

答案 0 :(得分:1)

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

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

网络api post方法

[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;}
}

答案 1 :(得分:0)

您可以尝试将代码更改为:

let body = {
   remark : remark
};

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

无需更改Web API。它应该工作。

答案 2 :(得分:0)

从@ angular / http

导入标题
Post=(url:string,data:any)=>{
    this.headers = new Headers();
    let options = new RequestOptions({ headers: this.headers, method: "post"}); 
    return this.http.post(
        url,
        data,
        options
    );
}
  

它对我有用;)