在asp.net MVC中,Angular 2 post数据为null

时间:2016-10-21 10:43:26

标签: c# asp.net post angular

我使用Angular 2和ASP.Net MVC。将数据(getData方法)发送到服务器时接收null。为了测试,我尝试使用jquery ajax发送帖子请求,一切顺利。 请告诉我这是什么问题?

properties.service.ts

[AllowAnonymous]

EditorController.cs

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

Screenshot c# debugScreenshot chrome-devtools

更新

更改 properties.service.ts 并且它有效。方法getData中的所有更改:

[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("");
    }
} 

3 个答案:

答案 0 :(得分:4)

更新您的控制器代码

[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("");
    }
} 

默认情况下,只从body中解析复杂参数。

答案 1 :(得分:0)

尝试这样的事情

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

答案 2 :(得分:0)

更改 properties.service.ts 并且它有效。方法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);
    }
}