asp.net mvc - Ajax POST to MVC Controller always erroring -
i have following ajax request:
$.ajax({ url: '/projects/index', type: 'post', data: data, datatype: 'json', contenttype: 'application/json; charset=utf-8', error: xhr => { swal('from error!', 'we\'ve saved hours, input!', 'success'); console.log(xhr) }, success: result => swal('from success!', 'we\'ve saved hours, input!', 'success') }); which, when status code 200, errors. controller receives data , stuff it, , returns view().
here's controller action in full:
[httppost, actionname("index")] public actionresult sethours(hourpostrequest[] data) { foreach(var item in data) { var correctmodel = item.toengineerhours(db); db.engineerhours.add(correctmodel); db.savechanges(); } var eng = db.engineers.firstordefault(e => e.domainusername == user.identity.name).id; var test = db.engineerhours.where(e => e.engineerid == eng).tolist(); return view(test); } the data intact, received fine , view returns. i've added in random test collection return model of view:
@model ilist<timesheet.models.engineerhours>
i've gone chrome dev-tools network view , payload correct, response 200 , controller has received data , returned view.
this means sweetalert2 alerts erroring regardless of success status.
can spot why might happening?
you returning view(), html string @ end of day. in request specify:
datatype: 'json' which tells jquery expect response json formatted string. jquery sees html , not able recognize valid json, hence error.
Comments
Post a Comment