![](/img/trans.png)
[英]Getting a 400 Bad Request error code in Angular 5 POST Call
[英]C# Angular app Post call produces 400 Bad Request
我正在開發一個從 do.net 模板創建的 angular 應用程序。 當我試圖發布到我的 controller 時,我收到一個 400 錯誤請求。我在 controller 上還有其他三種方法,都是 Get,它們工作得很好。
Controller:
[HttpPost("[action]")]
public async Task UpdateVehicle([FromBody]string content)
{
using(var client = new DocumentClient(
new Uri(config["cosmos:endpoint"]), config["cosmos:passkey"]))
{
await client.ReplaceDocumentAsync(JsonConvert.DeserializeObject<Document>(content));
}
}
按鈕事件:(在進行編輯時禁用設置為 false)
<div id="json-editor" class="col-md-7">
<button id=submitjson #savebutton [disabled]=true (click)=submitJson()>Save</button>
<json-editor [options]="options" [data]="currentFile"></json-editor>
</div>
零件:
@ViewChild('savebutton', { static: false }) saveButton: ElementRef;
@ViewChild(JsonEditorComponent, { static: false }) editor: JsonEditorComponent;
...
this.options.onChange = () => this.saveButton.nativeElement.disabled = false;
...
submitJson(){
var altered = JSON.stringify(this.editor.get());
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
this.http.post<any>(this.baseUrl + 'Ingestion/UpdateVehicle', this.editor.get(), httpOptions).subscribe(
{error: error => console.error('there was an error', error)}
);
}
我已經嘗試了enableCors路線,但它沒有用。 當我調試通過時,我發現錯誤:
error:
errors:
$: Array(1)
0: "The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1."
length: 1
__proto__: Array(0)
__proto__: Object
status: 400
title: "One or more validation errors occurred."
traceId: "|cfc492aa-4e4427da20e8bfb6."
type: "https://tools.ietf.org/html/rfc7231#section-6.5.1"
我在想這是當 .net 核心 controller 試圖將我的 json 值轉換為字符串參數時。 正如你們中的一些人可能會告訴我的那樣,我首先嘗試將其字符串化,但這沒有任何影響。
我昨天看到一篇關於 MS 為刪除 NewtonSoft 所做的替換的帖子,它導致了這個問題。 它與DeserialzeObject()
調用無關,因為我從來沒有走那么遠。 今天早上我找不到對它的引用,也不確定如何解決這個問題。 這是問題嗎? 有誰知道如何解決這一問題?
在這里找到了答案,但如果有人知道,我將不勝感激。 將 controller 更改為:
[HttpPost("[action]")]
public async Task UpdateVehicle([FromBody]Object content)
{
using(var client = new DocumentClient(
new Uri(config["cosmos:endpoint"]), config["cosmos:passkey"]))
{
await client.ReplaceDocumentAsync(JsonConvert.DeserializeObject<Document>(content.ToString()));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.