简体   繁体   中英

Jquery and ASP.NET ajax call to .NET is giving 500 error

I created an object like so:

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

and I am trying to send that object to .NET, but I am getting a 500 error, here is how I am sending this object:

 $.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');
            }
        });

the variable editObject is the object above. I don't think this part is the problem.

I am sending it to this controller in ASP.NET:

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

and then sending it this class:

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;
        }

Now here is something interesting when I remove the loop from my class so it looks like so:

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;
        }

this does not give me a 500 error, in fact I get the success alert from the ajax call. So I am assuming I am doing something wrong to loop through my object in .NET Does anyone have any suggestions ?

What I am expecting is vendorUpdatedItem.job equals ABC-123, vendorUpdatedItem.task equals ABC-123 and vendorUpdatedItem.vendor equals XYZ (for the first item anyways)

There is another problem...I can't debug my .NET code as my IIS is not working.

but here is the response I get from my network calls:

{"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(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--- 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.TaskAwaiter1.GetResult()\\r\\n at System.Web.Http.Controllers.ApiControllerA ctionInvoker.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.TaskAwaiter1.GetResult()\\r\\n at System.Web.Http.Controllers.ActionFilterResult.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.TaskAwaiter1.GetResult()\\r\\n at System.Web.Http.Controllers.AuthenticationFilterResult.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 S ystem.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\\r\\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"}

Your Javascript object should look something like this:

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

And your method definition should look like:

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
  }
}

Lastly, I recommend that you follow the proper naming conventions. For example, a public property should start with a capital letter.

string job = vendorUpdateItem.Job;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM