jquery ajax POST to ASP.NET

本文关键字:ASP NET to POST ajax jquery | 更新日期: 2023-09-27 17:50:46

我有这个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。网络控制器:

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的错误。为什么它会这样做,我该如何解决这个问题?

jquery ajax POST to ASP.NET

试试这个:

public List<VendorUpdateClass> updateVendorItem(string[] edittedItems)
    {
        ConnectionClass jobs = new ConnectionClass();
        return jobs.updateVendors(edittedItems);
    }

您也可以尝试用List<string>

替换字符串数组

我也发现了这一点,我如何张贴字符串数组的ASP。没有表单的。NET MVC控制器?

也许你能从中得到一些灵感

现在你张贴一个对象,包含一个属性称为edittedItems(旁注:你可能意味着editedItems与一个"t"),其值是一个数组。

与此形成对比的是你的控制器动作,它期望只是一个数组。您需要将AJAX请求更改为POST数组:
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 loop前写这行

if (items == null && items.Length < 1) return VendorUpdateCell;