Member_2_1242703
asked on
Function after success of Jquery/AJAX action
I'm submitting data using the below code...
It actually does work and the data gets into the DB, but the alert (ReqSuccess) is actually happening before the data is in the database. Is there something that stands out here that I'm doing wrong? I want the alert to happen after the data is actually in the table.
<script>
$(document).ready(function () {
$("#btnSave").click(function () {
$.ajax(
{
type: "POST",
url: "Home/AddPTO",
data: {
Name: $('#<%=lblEmployeeName.ClientID%>').text(),
Type: $('#<%=ddlRequestType.ClientID%>').val(),
StartDate: $('#<%=tbBeginDate.ClientID%>').val(),
EndDate: $('#<%=tbEndDate.ClientID%>').val(),
Hours: $('#<%=ddlHours.ClientID%>').val(),
Comments: $('#<%=tbComments.ClientID%>').val(),
EmpID: $('#<%=tbEID.ClientID%>').val(),
UpdateID: $('#<%=tbUpdateID.ClientID%>').val()
},
success: ReqSuccess()
});
});
})
function ReqSuccess() {
alert("Your request has been submitted");
}
</script>
It actually does work and the data gets into the DB, but the alert (ReqSuccess) is actually happening before the data is in the database. Is there something that stands out here that I'm doing wrong? I want the alert to happen after the data is actually in the table.
ASKER
Because the alert pops up, I don't click "OK" go and check the DB table, record isnt in there, click OK, go check again, record is there.
I am assuming given the fact you deleted your last comment that this is still an issue.
If you are seeing the Ok (alert) after the AJAX return - unless there is something you are not showing us - there is no link / relationship between the server side code and the alert box.
The alert will only show once the server script has completed and returned.
Check your console F12 when you do the AJAX request - look at the POST request there - check the Request tab and the Response tab to see if what is being sent and returned is what you expect.
If you are seeing the Ok (alert) after the AJAX return - unless there is something you are not showing us - there is no link / relationship between the server side code and the alert box.
The alert will only show once the server script has completed and returned.
Check your console F12 when you do the AJAX request - look at the POST request there - check the Request tab and the Response tab to see if what is being sent and returned is what you expect.
ASKER
Here is the code to my controller. I just left out the code that submits to the DB...
When I step through, the alert fires, then this part of the code is executed.
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace PTO.Controllers
{
public class HomeController : Controller
{
[HttpPost]
public ActionResult AddPTO(ReqModel obj)
{
//*******************This fires after the alert****************************
AddRequest(obj);
return View();
}
//To add Records into database
private void AddRequest(ReqModel obj)
{
//submit records here
}
}
}
When I step through, the alert fires, then this part of the code is executed.
When I step throughAnd if you are not debugging?
I suspect this is something specific to debugging.
Bottom line - client code (under normal conditions) does not fire until server code has finished.
My advice - test this without stepping through it with the debugger.
ASKER
Ok, so testing with the debugger, I was able to find a problem...
which is...
POST http://localhost:xxxxxx/Home/AddPTO 500 (Internal Server Error) on http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js:formatted:3762
which is...
u.send(n.hasContent && n.data || null),
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good idea! Ok so there was a misspelling in my view name. Once I changed that, boom, it worked fine. Strange that it was able to get through to the DB. I'm sure my setup is wrong and funky or something, but it is error free at this point. Thank you for the patience.
You are welcome.
The alert will fire only after the AJAX call completes - so unless the addition of data to the server is done asynchronously on the SERVER side then the chain of events should be
AJAX Request
Server side script runs
Adds to DB
Server side script terminates
AJAX returns
Success fires