In my ASP.NET web application, I have a simple Javascript function that executes on an input's onblur event:
function validateUsername() {
var request = new XMLHttpRequest();
if (request == null) {
alert("Failed to create request.");
} else {
var theName = document.getElementById("username").value;
var userName = encodeURIComponent(theName);
var url = "Default.aspx/CheckName?name='" + theName + "'";
request.onreadystatechange = handleStateChange(request);
request.open("GET", url, true);
request.setRequestHeader("Content-Type", "application/json");
request.send();
}
}
The C# method being called is as follows:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string CheckName(string name)
{
return name + " edited behind the scenes";
}
The callback function for the XMLHttpRequest is:
function handleStateChange(request) {
return function () {
if (request.readyState == 4) {
var parsed = JSON.parse(request.responseText);
alert(parsed.d);
}
}
}
While this does show me the outcomes of my server-side code, I am curious about the "d" property I'm accessing. Is it a standard way of retrieving parsed JSON data? Can this be done differently or more efficiently? Is "d" random or predefined? Can I change the property name either on the client side or server side?