0
我有一個問題,我的項目在角度& C#。我是一個begginer,我有這個問題。當我從angular發送JSON到C#(POST)時,雖然json有值,但c#沒有得到任何東西(值爲0,int爲null)。我不知道我做錯了什麼。將JSON具有這種結構:價值0或null從角度發送一個JSON到C#
[{"delegacion":11,"municipio":1,"ejercicio":2017,"ninterno":-1,"tipo":"T"}]
我的代碼
角
preparaTarea(){
this.data.push({'delegacion':this.delegacion,
'municipio':this.municipio,
'ejercicio':this.ejercicio,
'ninterno':this.ninterno,
'tipo':this.tipo_producto});
this._DelegationService.sendPtipo(this.data)
.subscribe(res=>{
this.data = res;
},
(err) =>console.log(err));
}
sendPtipo(data){
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let urlPtipo ='http://localhost:50790/ApiProductoTipo/CalculaPTRecinto';
let body = JSON.stringify(data);
console.log(body);
return this._http.post(urlPtipo , body , options)
.map(data => {alert('ok');})
.catch(this.handleError);
}
private handleError(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}}
C#
public class MyObj
{
public int delegacion { get; set; }
public int municipio { get; set; }
public int ejercicio { get; set; }
public int ninterno { get; set; }
public string tipo { get; set; }
}
[HttpPost]
[Route("~/ApiProductoTipo/CalculaPTRecinto")]
public HttpResponseMessage CalculaPTRecinto([FromBody]MyObj data)
{
var delegacion = data.delegacion;
var municipio = data.municipio;
var recinto = data.ninterno;
var ejercicio = data.ejercicio;
var tipo = data.tipo;
if (this.productoTipoService.CalculaPTRecinto(delegacion, municipio, recinto, ejercicio, tipo) != 0)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
當調試,delegacion的值,MUNICIPIO等具有0值,而tipo則爲null。有任何想法嗎?非常感謝!
嘗試的性質爲了照顧後硬編碼數據,並檢查,你不應該張貼陣列 –
這將是好得多,如果您使用郵差或提琴手首先檢查您的休息端點,併成功檢查後,繼續硬編碼的對象,然後在您的代碼 –