Angular4 http post方法不将数据传递给web api
当我调用http post方法时,它返回“数据未正确传递”。我通过body传递数据,并尝试使用json进行传递。stringify()我还尝试使用内容类型,如下所示。但它不起作用Angular4 http post方法不将数据传递给web api,angular,typescript,asp.net-web-api,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
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";
}
尝试将post数据作为对象传递,如下所示:
// 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;}
}
您可以尝试将代码更改为:
let body = {
remark : remark
};
this.http_.post('http://localhost:12412/api/employee',JSON.stringify(body),options)
.subscribe((data) => {console.log(data)});
不更改Web API。它应该工作。从@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
);
}
它对我有用;)
有什么错误吗?post here plz“备注”值不传递给web api post方法。当我编译代码时,它返回“数据未正确传递”。如果它正确传递,则需要返回我传递的“备注”值
console.log(备注+“”+log\u id)代码>将所需值打印到控制台?尝试通过邮递员或任何客户端发布数据。@E.L.N.D.Madubhashini您可以在没有选项参数的情况下尝试。@AliShahzad OP应该能够发送字符串值。我认为课堂是一种解决方法。我尝试使用上面更新的答案仍然没有工作。你从URL中删除了“?”吗?我试过不使用选项参数。它产生了一个名为“飞行前的响应具有无效的HTTP状态代码405”和“选项url 405(不允许使用方法)”的错误。我仍然无法获取该数据并传递消息。我添加了.subscribe((数据)=>{console.log(数据)});到上面的代码,检查这些数据是否正在传递。仍然没有通过。不要直接订阅http post(不推荐),从post返回数据。将其作为常用方法订阅。喜欢this.httpservice.postdata().subscribe()