I'm trying to create a username-check that sends a username to the server which will answer true or false. Every time sb left the textbox it sends the username. My idea was to create a ajax method that call a codebehind c# method. But it doesn't work.
Information: It is a WebForms Application
ASPX Code:
<script type="text/javascript" src="/scripts/jquery-1.4.1.js"></script> <script type="text/javascript"> function checkUsername() { $.ajax({ type: 'POST', url: 'Default.aspx/checkUsername', data: JSON.stringify({ name: document.getElementById('txt_Username').value }), contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (msg) { // (inform the user) alert(msg); }, error: function (err) { alert(err); } }); }); </script>
And the CodeBehind
[WebMethod]
public static string checkUsername(string name)
{
//Get the server data
return output;
}
I tried a lot of things with this code but nothing works!
change this
function checkUsername() {
$.ajax({
type: 'POST',
url: 'Default.aspx/checkUsername',
data: JSON.stringify({ name: document.getElementById('txt_Username').value }),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
// (inform the user)
alert(msg);
},
error: function (err) {
alert(err);
}
});
});
to
function checkUsername() {
var obj = { "name": $('txt_Username').val() }
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/checkUsername",
data: JSON.stringify(obj),
dataType: "json",
success: function (msg) {
// (inform the user)
alert(msg);
},
error: function (err) {
alert(err);
}
});
});
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.