Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular PUT和POST方法在Postman中有效,但只有POST方法在10中有效_Angular_Visual Studio_Webapi_Angular10 - Fatal编程技术网

Angular PUT和POST方法在Postman中有效,但只有POST方法在10中有效

Angular PUT和POST方法在Postman中有效,但只有POST方法在10中有效,angular,visual-studio,webapi,angular10,Angular,Visual Studio,Webapi,Angular10,这在我的“编辑大陆.component.ts”中 添加大陆(大陆数据){ this.contraction.name=contractionData.name; this.continentService.AddContinental(this.Continental).subscribe((响应)=>{ (数据)=> (这个大陆={ id:(数据如有)。id, 名称:(数据如有)。名称, }); this.ngOnInit()、this.groundForm.reset(); }); } 编辑

这在我的“编辑大陆.component.ts”中

添加大陆(大陆数据){
this.contraction.name=contractionData.name;
this.continentService.AddContinental(this.Continental).subscribe((响应)=>{
(数据)=>
(这个大陆={
id:(数据如有)。id,
名称:(数据如有)。名称,
});
this.ngOnInit()、this.groundForm.reset();
});
}
编辑大陆(大陆数据){
this.contraction.id=this.contractionid;
this.contraction.name=contractionData.name;
log(`${this.consolide}`);
log(`${columentdata.name}`);
console.log(这个大陆);
this.continentService.EditContinental(this.Continental).subscribe((响应)=>{
(数据)=>
(这个大陆={
id:(数据如有)。id,
名称:(数据如有)。名称,
});
this.ngOnInit()、this.groundForm.reset();
});
}
(console.logs所有日志都打印它们应该打印的内容)
这是我的“大陆服务ts”
大陆(大陆:大陆):可见{
返回此文件。http
.post(this.url,contraction,httpOptions)
.pipe(重试(2),catchError(this.handleError));//tenta Novatemente por 2x
}
编辑大陆(大陆:大陆):可观察{
const apirl=`${this.url}/${contraction.id}`;
控制台.日志(大陆);
console.log(apirl);
返回此文件。http
.put(apiurl、大陆、httpOptions)
.pipe(重试(2),catchError(this.handleError));
}
(console.logs还打印它们应该打印的内容) 这在我的VisualStudio Web API中(使用Dapper和SQL Server)

[HttpPost]
公共IActionResult后大陆(大陆)
{
int i=大陆储存库。后大陆(大陆);
d=i;
如果(i>0)
返回Ok(大陆);
返回NotFound();
}
[HttpPut(“{id}”)]
大陆公共IActionResult(长id,大陆)
{
Console.WriteLine(id.ToString(),大陆);
if(id!=大陆.id)
返回请求();
int i=大陆存储库。PutContinental(大陆);
如果(i>0)
返回Ok(大陆);
返回NotFound();
}
它们在Postman中工作,但只有POST方法在Angular中工作,PUT方法给了我以下错误:

    X PUT https://localhost:44306/continent/4 400
    X Backend returned code 400, body was: [object Object]
    X Uncaught TypeError: Cannot read property 'ngOriginalError' of undefined
        at getOriginalError (core.js:4346)
        at ErrorHandler._findOriginalError (core.js:4418)
        at ErrorHandler.handleError (core.js:4395)
        at Object.next (core.js:28037)
        at SafeSubscriber.schedulerFn [as _next] (core.js:24851)
        at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
        at SafeSubscriber.next (Subscriber.js:122)
        at Subscriber._next (Subscriber.js:72)
        at Subscriber.next (Subscriber.js:49)
        at EventEmitter_.next (Subject.js:39)
我的api启动时也有addcor

public void配置服务(IServiceCollection服务)
{
[...]
services.AddCors(c=>
{
c、 AddPolicy(“AllowOrigin”,options=>options.AllowAnyOrigin());
});
services.AddControllers();
}
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
app.UseCors(options=>options.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
[...]
}
我尝试了几种不同的方法,但不知道为什么会出现这样的错误,我想可能是关于我作为参数传递的对象“大陆”,但由于它适用于POST(也适用于GET和DELETE,但我认为它不相关),我不明白为什么它不适用于PUT

编辑: 我尝试在[HttpPut(“{id}”)中使用[FromBody],但没有成功。使用“动态”而不是“大陆”时,可以看到:

ValueKind = Object : "{"id":"90002","name":"oi"}"
error CS0726: ':' is not a valid format specifier

我该怎么办?

在你的put中,你需要告诉它大陆来自身体


public-IActionResult-putcontricent(长id,[FromBody]contricent-contricent)
事实证明,这就是我发送的id类型。它是字符串格式,而不是long,但我以前没有注意到。这就解决了问题

editContinent(continentData) {

    this.continent.id = Number(this.continentId);
    this.continent.name = continentData.name;

    this.continentService
      .editContinent(this.continent)
      .subscribe((data : Continent) =>
      {
          this.continent = data;
          this.continentForm.reset();
      });
  }

HTTP状态代码400是错误的请求,如果URL中指定的id与正文中给定的
大陆
对象中指定的id不同,则返回该请求。我想这就是我要开始的地方…@HereticMonkey我试着删除错误的请求,删除所有的代码,只返回字符串或对象本身,没有任何效果。我认为它甚至没有进入put方法实际上,看到这个错误让我觉得有什么不对劲
body是[object object]
似乎在说数据没有字符串化。我以为Angular的HttpClient自动做到了。。。可能是
httpOptions
中的某些内容?将api上的输入类型更改为动态,然后在条目上更改断点。这允许您查看它是否是服务器端绑定问题。@JohnPeters尝试了动态,api给了我:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“System.Text.Json.JsonElement”不包含“Id”的定义。
。所以它没有接收到物体。也许我需要在
httpOptions
中更改一些内容,就像@HereticMonkey所说的那样。