[英]Http post Angular2
I am migrating an Ionic v1 application to Ionic v2. 我正在将Ionic v1应用程序迁移到Ionic v2。 In one of my methods I make a call to a WCF service.
在我的一种方法中,我调用了WCF服务。 In AngularJS I do it like this:
在AngularJS中,我这样做:
$http({
method: 'POST',
url: url,
data: JSON.stringify({ dato: 11 }),
dataType: "json",
contentType: "application/x-www-form-urlencoded"
})
And in Angular2 like this: 在Angular2中是这样的:
let data = JSON.stringify({ dato: 11 });
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded'});
let options = new RequestOptions({ headers: headers });
this.http.post(url, data, options)
.subscribe(data => {
console.log(data);
}, error => {
console.log("Error");
});
However, I receive from my C# service an error and says that the format I send to my functions is 'RAW' while waiting for it to send JSON with Angular 2 但是,我从C#服务收到一个错误,并说我在等待它使用Angular 2发送JSON时发送给函数的格式为'RAW'
This error occurred to me in AngularJS but it was solved by adding the code I previously left in this post. 我在AngularJS中发生了此错误,但是通过添加我之前在本文中留下的代码解决了该错误。
EDIT 编辑
My WCF service : 我的WCF服务:
[OperationContract]
[WebInvoke(UriTemplate = "/GetCaptaciones", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json , BodyStyle = WebMessageBodyStyle.Wrapped)]
List<Captacion> GetCaptaciones(int id_captador);
AND 和
let data = JSON.stringify({ id_captador: 11 });
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.post(url + "/GetCaptaciones", data, options).subscribe(data => {
console.log(data);
});
Not working, 不工作
But if I test from POSTMAN this works 但是如果我从POSTMAN进行测试,则可以
This: 这个:
let data = JSON.stringify({ dato: 11 });
and this: 和这个:
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded'});
are terribly contradictory and inconsistent. 非常矛盾和前后矛盾。
Please try to be consistent when you make an HTTP request against a web server about how you are going to encode the request. 当您对网络服务器发出HTTP请求时,请尝试保持一致,以了解如何对请求进行编码。 So if intend to send a JSON body make sure that you specify the proper
Content-Type
request header, so that the server would know how to process this payload: 因此,如果打算发送JSON正文,请确保指定正确的
Content-Type
请求标头,以便服务器知道如何处理此有效负载:
let headers = new Headers({ 'Content-Type': 'application/json'});
In addition to what Darin has said I would say you are not using BodyStyle = WebMessageBodyStyle.Wrapped
correctly. 除了达林所说的以外,我还要说的是您没有正确使用
BodyStyle = WebMessageBodyStyle.Wrapped
。
See your wcf method expects a single parameter so you could go ahead without wrappedrequest property. 请参阅您的wcf方法需要一个参数,这样您就可以不使用wrappedrequest属性。
[OperationContract]
[WebInvoke(UriTemplate = "/GetCaptaciones", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json)]
List<Captacion> GetCaptaciones(int id_captador);
Wrapped request means there should be some wrapper object on top of the input properties which WCF method takes as argument. 包装的请求意味着在输入属性的顶部应该有一些包装对象,WCF方法将其作为参数。
So something like this 所以像这样
var input =
{
"CaptacionesInput":
{
"id_captador": 11
}
};
Now wcf method becomes like 现在,wcf方法变得像
List<Captacion> GetCaptaciones(CaptacionesInputType CaptacionesInput);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.