[英]jquery ajax POST to ASP.NET
我有這個ajax調用,並且正在向其中傳遞一個看起來像["ANC0001-Pin Lot"]
的數組,然后嘗試將其傳遞給asp.net。 我可以在網絡控制台中的“請求有效負載”下看到我的陣列在那里。
這是jQuery:
$.ajax({
type: "POST",
url: "/vendorProject/api/connection/updateVendorItem",
data: JSON.stringify({ edittedItems: editHolder }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert('success');
},
failure: function (errMsg) {
alert('failed');
}
});
這是我的ASP.NET控制器:
public List<VendorUpdateClass> updateVendorItem(Array edittedItems)
{
ConnectionClass jobs = new ConnectionClass();
return jobs.updateVendors(edittedItems);
}
updateVendors
方法:
public List<VendorUpdateClass> updateVendors(Array items)
{
VendorUpdateCell = new List<VendorUpdateClass>();
VendorUpdateClass vendorUpdatedItem = new VendorUpdateClass();
for (int i = 0; i < items.Length; i++ ){
vendorUpdatedItem.job = "aaa";
vendorUpdatedItem.task = "bbb";
vendorUpdatedItem.vendor = "ccc";
VendorUpdateCell.Add(vendorUpdatedItem);
}
return VendorUpdateCell;
}
我正在獲取500內部服務器錯誤:
{
"Message":"An error has occurred.",
"ExceptionMessage":"Object reference not set to an instance of an object.",
"ExceptionType":"System.NullReferenceException",
"StackTrace":" at VendorProject.Models.ConnectionClass.updateVendors(Array items)\r\n
at lambda_method(Closure , Object , Object[] )\r\n
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary 2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\r\n
at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\r\n
at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\r\n at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\r\n
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}
沒有循環,它確實起作用。 當我放入循環時,這給了我500錯誤。 為什么要這樣做,我該如何解決?
嘗試這個:
public List<VendorUpdateClass> updateVendorItem(string[] edittedItems)
{
ConnectionClass jobs = new ConnectionClass();
return jobs.updateVendors(edittedItems);
}
您也可以嘗試用List<string>
替換字符串數組
我還發現了如何不使用表單將字符串數組發布到ASP.NET MVC Controller?
也許你在那里可以得到一些啟發
現在,您要發布一個對象,該對象包含一個名為edittedItems
的屬性(注意:您可能是指帶有一個“ t”的editedItems
),其值是一個數組。
將此與您期望僅一個數組的控制器操作進行對比。 您需要將AJAX請求更改為僅發布一個數組:
data: JSON.stringify(editHolder)
然后就像@JOSEFtw所提到的 ,您可能應該使用string[]
或List<string>
而不是Array
。
現在嘗試此方法,我已經更新了id,因為我可能由於空引用items.Length
而崩潰。
public List<VendorUpdateClass> updateVendors(Array items)
{
VendorUpdateCell = new List<VendorUpdateClass>();
VendorUpdateClass vendorUpdatedItem = new VendorUpdateClass();
if (items == null && items.Length < 1)
return VendorUpdateCell;
for (int i = 0; i < items.Length; i++)
{
vendorUpdatedItem.job = "aaa";
vendorUpdatedItem.task = "bbb";
vendorUpdatedItem.vendor = "ccc";
VendorUpdateCell.Add(vendorUpdatedItem);
}
return VendorUpdateCell;
}
在for
循環之前寫這行
if (items == null && items.Length < 1) return VendorUpdateCell;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.