有人能告诉我为什么在Fiddler的请求正文中看到引号而我的服务栈请求字段中没有?
POST http://10.255.1.180:8087/testvariables/new/ HTTP/1.1
Host: 10.255.1.180:8087
Connection: keep-alive
Content-Length: 162
Origin: http://10.255.1.180:8087
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36
content-type: application/json;charset=UTF-8
Accept: */*
Referer: http://10.255.1.180:8087/certFields
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,fr;q=0.6
{"Field":{"Id":67,"Name":"Brightness Reversion","Header":"Brightness Reversion.","Format":"#0.0","Uom":"%","Precision":0,"MapId":8083,"IsMappedToCustomer":false}}
从Typescript / Angular2调用
let body = JSON.stringify(certField);
let headers = new Headers({ 'Content-Type': 'application/json;charset=utf-8' });
let requestoptions: RequestOptions = new RequestOptions({
method: RequestMethod.Post,
url: this._certFieldsUrl + 'new/',
headers: headers,
body: '{"Field":'+body+'}'
})
return this._http.request(new Request(requestoptions))
.toPromise()
.then(function (response) {
return response.json() as CertFieldSaveResponse;
})
.catch(this.handleError);
请求类
public class CertFieldUpdateRequest : IReturn<TestVariableResponse>
{
public string Field { get; set; }
}
POST方法
public object Post(CertFieldUpdateRequest request)
{
log.Debug(request.Field);
....
}
在服务内部,在POST方法中,request.Field的值为:
{Id:67,Name:Brightness Reversion,Header:Brightness Reversion.,Format:#0.0,Uom:%,Precision:0,MapId:8083,IsMappedToCustomer:false}
答案 0 :(得分:2)
因为你的身体实际上是一个完整的json对象而不是json对象中的字符串。所以:
let requestoptions: RequestOptions = new RequestOptions({
method: RequestMethod.Post,
url: this._certFieldsUrl + 'new/',
headers: headers,
body: {Field : JSON.stringify(body)}
})
或者让您的代码类具有属性:
public class CertFieldUpdateRequest : IReturn<TestVariableResponse>
{
public Field Field { get; set; }
}
public class Field
{
public int Id { get; set; }
public string Name { get; set; }
public string Header { get; set; }
public string Format { get; set; }
///etc...
}