I have two aspx page on my application. From the first one, lets say default.aspx, I have one button and one textbox. When I click the button I do an ajax call to the checkname.aspx as below
function docheck() {
var un = $('#username').val();
$.ajax({
url: "checkname.aspx",
data: { name: un, method: 'checkname' },
dataType: 'script',
success: function (val) {
if (val == "No")
alert("not valid");
else
alert("valid!");
}
});
}
From checkname.aspx page I do the operations like below but it response is also adding the checkname.aspx page’s html to the response so the success message of ajax call doesnt became what I wanted. How do I send just what the CheckName method send.
checkname.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
NameValueCollection queries = HttpUtility.ParseQueryString(Request.QueryString.ToString());
if(queries["method"] == "checkname")
this.CheckName(queries["name"]);
}
protected void CheckName(string Name)
{
if (Name == "ryu")
{
HttpContext.Current.Response.Write("No");
return;
}
HttpContext.Current.Response.Write("Yes");
}
Thanks in advance,
Call
HttpContext.Current.Response.End();to end processing your aspx request, otherwise, all stuff after that call in Page_Load gets processed normally, thus you’ve got the results you’ve described.But in general, follow the advices given by other people here, and work with your AJAX requests in different way.