HTML:
<div id="api-calls" class="ui-drop-shadow">
<button id="ajax" class="form-control">ajax call</button>
</div>
jQuery 呼叫如下:
on(ajax, "click", function(event) {
alert('ajax');
$.ajax({
type: "GET",
data: {SiteId: 8},
dataType: "json",
//dataType: "application/json, text/plain, */*",
//dataType: "jsonp",
//url: "https://localhost:44310/api/site/search?SiteId=8",
url: "https://localhost:44310/api/site/search",
success: function(data){
alert(data);
}
});
});
ASP.NET 站點控制器:
[HttpGet("[action]")]
public async Task<ActionResult<List<ApiObject>>> Search(int? SiteId)
{
//Database logics that get to the 'recList' record list... all worked
// finally, construct a list of the ApiObject for return, this is where the call failed
// every time.
recAPIList = recList.Select(a => new ApiObject()
{
ApiObjectId = a.ObjectId,
Description = a.Description,
Name = a.Name,
IsDisabled = a.IsDisabled,
})
.ToList();
return Ok(recAPIList);
}
如您所見,我已經dataType在 ajax 呼叫中嘗試了各種組合,但是所有這些組合都在 ASP.NET 控制器中構造回傳 APIObjects 串列的最后一步失敗。
檢查后的網路回應給出了以下錯誤:
{"Type":null,"Title":"An unexpected error occurred!","Status":500,
"Detail":"The Instance value should be used to identify the problem when contacting support.\r\n
System.InvalidOperationException: Sequence contains more than one element\r\n
at System.Linq.ThrowHelper.ThrowMoreThanOneElementException()\r\n
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)\r\n
at Controllers.SiteController.<>c__DisplayClass5_0.<Search>b__7(Site a)
in \\Controllers\\SiteController.cs:line 167\r\n
at System.Linq.Enumerable.SelectListIterator`2.ToList()\r\n
at lambda_method(Closure , Object )\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)\r\n
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)\r\n
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)\r\n
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\r\n
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)","Instance":"urn:nebraskadss:error:5eae0ea1-af8a-4cb9-9532-2060c922af93","Extensions":{}}
我還嘗試在控制器.ToList() Linq中用 for 回圈替換該陳述句以創建 ApiObjects 串列,但仍然以相同的錯誤回應失敗。
我想補充一點,這個端點來自一個現有的作業應用程式,它具有 asp.net core 后端和 angular 前端,它用于檢索物件串列。我發布這個問題的唯一原因是這個例外只發生在我從 jQuery 呼叫它時!
uj5u.com熱心網友回復:
如果你仔細閱讀:
序列包含多個元素
和
在 System.Linq.Enumerable.Single
這意味著您正在.Single()對 Collection 或 Enumerable 物件進行操作,這是允許的,但是該 Collection 或 Enumerable 物件會產生2 個或更多 items。
Single()強制 1 結果。如果它找到 0 個結果,或者 2 個或更多結果,那么它會拋出錯誤。您要么需要修復集合,或者如果不可能,請考慮使用First()代替Single().
最后,看起來錯誤也是由您未顯示的代碼引起的。如果recList是查詢的結果,那么您需要修復該查詢。
uj5u.com熱心網友回復:
你必須改變ajax請求
$.ajax({
url: "https://localhost:44310/api/site/search?siteId=8",
type: "GET",
dataType: "json",
success: function(data){
alert( JSON.stringify(data));
}
或保留 ajax 但更改操作標題
[HttpGet("~/api/site/search/{siteId?}")]
public async Task<ActionResult<List<ApiObject>>> Search(int? siteId)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/347667.html
標籤:查询 阿贾克斯 asp.net-core-webapi
下一篇:使用onclick更新
