Jquery和ASP..NET ajax调用.NET给出500错误

本文关键字:NET 给出 错误 调用 ajax ASP Jquery | 更新日期: 2023-09-27 17:51:00

我创建了一个像这样的对象:

Object {ABC-123: "XYZ", ABC-112: "LAX"}

,我试图发送该对象到。net,但我得到一个500错误,这是我如何发送这个对象:

 $.ajax({
            type: "POST",
            url: "/vendorProject/api/connection/updateVendorItem",
            data: JSON.stringify(editObject),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                alert('success');
            },
            failure: function (errMsg) {
                alert('failed');
            }
        });

变量editObject就是上面的对象。我认为这不是问题所在。

我在ASP中将它发送到这个控制器。净:

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

然后发送这个类:

public List<VendorUpdateClass> updateVendors(string[] items)
        {
            VendorUpdateCell = new List<VendorUpdateClass>();
            VendorUpdateClass vendorUpdatedItem = new VendorUpdateClass();
            for (int i = 0; i < items.Length; i++ ){
                vendorUpdatedItem.job = items[i];
                vendorUpdatedItem.task = items[i];
                vendorUpdatedItem.vendor = items[i];
                VendorUpdateCell.Add(vendorUpdatedItem);
            }
            return VendorUpdateCell;
        }

现在有一些有趣的事情当我从类中删除循环时,它看起来像这样:

public List<VendorUpdateClass> updateVendors(string[] 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错误,事实上,我从ajax调用中获得成功警报。所以我假设我在。net中循环我的对象时做错了什么,有人有任何建议吗?

我期待的是vendorUpdatedItem.job等于ABC-123, vendorUpdatedItem.task等于ABC-123和vendorUpdatedItem.vendor等于XYZ(无论如何,对于第一项)

还有一个问题……我无法调试我的。net代码,因为我的IIS不工作。

但这是我从网络调用中得到的响应:

{"Message":" error has occurred.","ExceptionMessage":"Object reference not set to An Object instance .","ExceptionType":"System. error ."NullReferenceException","StackTrace":" at VendorProject.Models.ConnectionClass. "updateVendors(String[] items)'r'n at lambda_method(Closure, Object, Object[])'r'n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10。b__9(Object instance, Object[] methodParameters)'r'n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor。ExecuteAsync(HttpControllerContext controllerContext, IDictionary2 arguments, CancellationToken CancellationToken)'r'n——从抛出异常的前一个位置开始的堆栈跟踪的结束——'r'n at System.Runtime.CompilerServices.TaskAwaiter. ExecuteAsync(HttpControllerContext, controllerContext, IDictionary2 arguments)'r'n在System.Runtime.CompilerServices.TaskAwaiter。'r'n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()'r'n——从抛出异常的前一个位置开始的堆栈跟踪结束——'r'n at system . runtime . compilerservices . taskawaiter1 . gettresult ()'r'n at system . runtime . compilerservices . taskawaiter1。'r'n在System.Runtime.CompilerServices.TaskAwaiter。'r'n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()'r'n——从抛出异常的前一个位置开始的堆栈跟踪结束——'r'n at system . runtime . compilerservices . taskawaiter1 . gettresult ()'r'n at system . runtime . compilerservices . taskawaiter1 . gettresult ()'r'n'r'n在System.Runtime.CompilerServices.TaskAwaiter。'r'n at System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()'r'n——从抛出异常的前一个位置开始的堆栈跟踪的结束——'r'n at system . runtime . compilerservices . taskawaiter1 . gettresult ()'r'n'r'n在System.Runtime.CompilerServices.TaskAwaiter。'r'n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()'r'n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"}

Jquery和ASP..NET ajax调用.NET给出500错误

你的Javascript对象应该看起来像这样:

var vendorUpdatLists= [ { job: "ABC-123", task: "XYZ" },  { job: "ABC-333", task: "LAX" }];

你的方法定义应该是这样的:

public List<VendorUpdateClass> updateVendors(List<VendorUpdateClass> vendorUpdateItems) {
  // It's time to use the list!
  if (vendorUpdateItems != null && vendorUpdateItems.Count() > 0) {
    string job = vendorUpdateItems[0].job; // ABC-123
    string task = vendorUpdateItems[0].task; // XYZ
  }
}

最后,我建议您遵循正确的命名约定。例如,公共属性应该以大写字母开头。

string job = vendorUpdateItem.Job;