2

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);
    }

3 Answers 3

4

Update your controller code

[HttpPost]
public JsonResult returnProperties([FromBody]string FullName) // <--- Added FromBody
{
    try
    {
        PAVBaseDataItem node = IDE.MvcApplication.fDataProvider.RootItem.ChildItem(FullName);
        List<PAVBasePropertyItem> properties = node.PropertiesList();
        return Json(properties);
    }
    catch (Exception ex)
    {
    return Json("");
    }
} 

Only complex parameters are resolved from body by default.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi, tell please the name space for [FormBody]?
System.Web.Http
Fixed as in your example, but on server receive null.
Can you check on developer console to see if your requests body is really added and what is inside?
Breakpoint catches the request, but I do not see its value in the console visual studio.
|
0

Try something like this

   const headers = new Headers();
    headers.append('Content-Type', 'application/json');

    this.http.post(this.dataUrl, JSON.stringify(data), { headers: headers })      //added return
        .map(response => response.json())
        .subscribe(result => {
            var returneddata = result;
        },
        err => console.log(err)
        );

Comments

0

Changed the properties.service.ts and it worked. All changes in the method getData:

import { Injectable }     from '@angular/core';
import { Http, Response } from '@angular/http';
import { Headers, RequestOptions } from '@angular/http';

import { PropertyNode } from './property.model';

@Injectable()
export class PropertiesService {
    private dataUrl = 'Editor/returnProperties';

    constructor(private http: Http) { }

    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);
    }

    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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.