I have a piece of code that inserts some contact details using ajax and c# The code runs through and the db is updated with the record but the alert box for a success doesnt seem to show. It does seem to work if I debug through the javascript debugger in chrome, it will hit that piece of code and the alert will show. But not otherwise.
Here is the code:
Javascript
function insertDonator() {
var title = $("#DropDownTitle option:selected").text();
var FirstName = $("#txtFirstNameinput").val();
var LastName = $("#txtLastNameInput").val();
var Add1 = $("#txtAdd1").val();
var Add2 = $("#txtAdd2").val();
var Town = $("#txtTown").val();
var County = $("#txtCounty").val();
var PostCode = $("#txtPostCode").val();
var telephone = $("#txtPhoneInput").val();
var email = $("#txtEmailInput").val();
var collectionArea = $("#txtCollectionAreaInput").val();
$.ajax({
type: "POST",
url: "WebService1.asmx/insertDonator",
data: '{title:"' + title + '", FirstName: "' + FirstName + '",LastName:"' + LastName + '", Add1:"' + Add1 + '", Add2: "' + Add2 + '",Town:"' + Town + '",County:"' + County + '",PostCode:"' + PostCode + '",telephone:"' + telephone + '",email:"' + email + '",collectionArea:"' + collectionArea + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
Apprise(response.d);
}
}
)
}
function OnSuccess(response) {
alert("Record added");
}
HTML (aspx file)
<asp:Button ID="btnaddRecord" runat="server" Text="Add Donator" OnClientClick="insertDonator();" />
You are not cancelling the default action of the button click. Normally you can use preventDefault() , but you can also retrun false in the click event.
OnClientClick="insertDonator(); return false;"
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.