I use Angular 2 and ASP.Net MVC. When sending data(the getData method) to the server receive null. For the test, I tried to send a post request using jquery ajax, everything went well. Please tell me what is the problem?
properties.service.ts
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Headers, RequestOptions } from '@angular/http';
import '../../../shared/rxjs-operators';
import { PropertyNode } from './property.model';
@Injectable()
export class PropertiesService {
private headers = new Headers({ 'Content-Type': 'application/json' });
private dataUrl = 'Editor/returnProperties';
constructor(private http: Http) { }
getData(FullName: string): Promise<PropertyNode[]> {
let body = JSON.stringify({ FullName: FullName});
let options = new RequestOptions({
headers: this.headers
});
return this.http.post(this.dataUrl, body, this.headers)
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response): PropertyNode[] {
let data = res.json();
let properties: PropertyNode[] = [];
console.log(data)
return data;
}
private handleError(error: any) {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
EditorController.cs
[HttpPost]
public JsonResult returnProperties(string FullName)
{
try
{
PAVBaseDataItem node = IDE.MvcApplication.fDataProvider.RootItem.ChildItem(FullName);
List<PAVBasePropertyItem> properties = node.PropertiesList();
return Json(properties);
}
catch (Exception ex)
{
return Json("");
}
}
Screenshot c# debug, Screenshot chrome-devtools.
Update
Changed the properties.service.ts and it worked. All changes in the method getData:
getData(FullName: string): Promise<PropertyNode[]> {
let body = JSON.stringify({ FullName });
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(this.dataUrl, body, options)
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}