gpt4 book ai didi

c# - Angular 2 发布数据在 asp.net MVC 中为空

转载 作者:太空宇宙 更新时间:2023-11-03 21:07:35 24 4
gpt4 key购买 nike

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

属性.服务.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 .

更新

更改了 properties.service.ts 并且它起作用了。方法 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);
}

最佳答案

更新你的 Controller 代码

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

默认情况下,只有复杂的参数从正文中解析出来。

关于c# - Angular 2 发布数据在 asp.net MVC 中为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40174552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com