I want to call ajax function with the jQuery. Here is my sample coding
<script src="js/jquery-1.4.1.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
debugger
$("#btnSubmit").click(function(e) {
e.preventDefault();
if ($("#txtUserName").val() == '')
alert("Please enter the UserName");
else
sendData($("#txtUserName").val());
});
function sendData(sUserName) {
debugger
var loc = window.location.href;
loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "Recipe3.aspx" : loc;
' $.ajax({
debugger
type: "POST",
url: loc + "/CheckUserName",
data: '{"sUserName":"' + sUserName + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
if (msg.d)
alert("The User Name is valid");
else
alert("The User Name is invalid")
},
error: function() {
alert("An unexpected error has occurred during processing.");
}
});
}
});
</script>
What does this mean: loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "Recipe3.aspx" : loc;
Apparently, user can access the page with “http://www.yourDomain.com/someDir/”, or with “http://www.yourDomain.com/someDir/Recipe3.aspx”.
the server (i guess iis) take charge of calling recipe3.aspx when the “http://www.yourDomain.com/someDir/”.
However, the ajax call url is “http://www.yourDomain.com/someDir/recipe3.aspx/CheckUserName”, not “http://www.yourDomain.com/someDir//CheckUserName”.
this if statement makes sure the correct ajax url target is the good one.