You could try taking the "A" out of "AJAX".
Add the script below, and then change your PageMethods.EmailAddressIsValid call to SyncPageMethods.EmailAddressIsValid .
Type.registerNamespace("System.Net");
System.Net.XMLHttpSyncExecutor = function()
{
if (arguments.length !== 0) { throw Error.parameterCount(); }
System.Net.XMLHttpSyncExecutor.initializeBase(this);
};
System.Net.XMLHttpSyncExecutor.prototype =
{
executeRequest: function()
{
if (arguments.length !== 0) { throw Error.parameterCount(); }
var webRequest = this.get_webRequest();
if (this._started) { throw Error.invalidOperation(String.format(Sys.Res.cannotCallOnceStarted, "executeRequest")); }
if (null === webRequest) { throw Error.invalidOperation(Sys.Res.nullWebRequest); }
var body = webRequest.get_body();
var headers = webRequest.get_headers();
this._xmlHttpRequest = new XMLHttpRequest();
this._xmlHttpRequest.onreadystatechange = this._onReadyStateChange;
var verb = this._webRequest.get_httpVerb();
this._xmlHttpRequest.open(verb, this._webRequest.getResolvedUrl(), false);
if (headers)
{
for (var header in headers)
{
var val = headers[header];
if (typeof(val) !== "function")
{
this._xmlHttpRequest.setRequestHeader(header, val);
}
}
}
if ("post" === verb.toLowerCase())
{
if (null === headers || !headers["Content-Type"])
{
this._xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
}
if (!body)
{
body = "";
}
}
this._xmlHttpRequest.send(body);
if (null !== this._xmlHttpRequest)
{
this._responseAvailable = true;
webRequest.completed(Sys.EventArgs.Empty);
this._xmlHttpRequest.onreadystatechange = Function.emptyMethod;
this._xmlHttpRequest = null;
}
}
};
System.Net.XMLHttpSyncExecutor.registerClass("System.Net.XMLHttpSyncExecutor", Sys.Net.XMLHttpExecutor);
System.Net.WebServiceSyncProxy = function()
{
if (arguments.length !== 0) { throw Error.parameterCount(); }
System.Net.WebServiceSyncProxy.initializeBase(this);
};
System.Net.WebServiceSyncProxy.prototype =
{
_invokeSync: function(servicePath, methodName, useGet, params)
{
var e = Function._validateParams(arguments, [
{name: "servicePath", type: String},
{name: "methodName", type: String},
{name: "useGet", type: Boolean, optional: true},
{name: "params", mayBeNull: true, optional: true}
]);
if (e) { throw e; }
return System.Net.WebServiceSyncProxy.invokeSync(servicePath, methodName, useGet, params);
}
};
System.Net.WebServiceSyncProxy.invokeSync = function(servicePath, methodName, useGet, params)
{
var e = Function._validateParams(arguments, [
{name: "servicePath", type: String},
{name: "methodName", type: String},
{name: "useGet", type: Boolean, optional: true},
{name: "params", mayBeNull: true, optional: true}
]);
if (e) { throw e; }
var request = new Sys.Net.WebRequest();
request.set_executor(new System.Net.XMLHttpSyncExecutor());
request.get_headers()["Content-Type"] = "application/json; charset=utf-8";
if (!params) { params = {}; }
var urlParams = params;
if (!useGet || !urlParams) { urlParams = {}; }
request.set_url(Sys.Net.WebRequest._createUrl(servicePath + "/" + encodeURIComponent(methodName), urlParams));
var body = null;
if (!useGet)
{
body = Sys.Serialization.JavaScriptSerializer.serialize(params);
if (body === "{}") { body = ""; }
}
request.set_body(body);
var result = null;
request.add_completed(onComplete);
request.invoke();
function onComplete(response, eventArgs)
{
if (response.get_responseAvailable())
{
var statusCode = response.get_statusCode();
try
{
var contentType = response.getResponseHeader("Content-Type");
if (contentType.startsWith("application/json"))
{
result = response.get_object();
}
else if (contentType.startsWith("text/xml"))
{
result = response.get_xml();
}
else
{
result = response.get_responseData();
}
}
catch (ex)
{
}
var error = response.getResponseHeader("jsonerror");
var errorObj = (error === "true");
if (errorObj)
{
if (result)
{
result = new Sys.Net.WebServiceError(false, result.Message, result.StackTrace, result.ExceptionType);
}
}
else if (contentType.startsWith("application/json"))
{
if (!result || typeof(result.d) === "undefined")
{
throw Sys.Net.WebServiceProxy._createFailedError(methodName,
String.format(Sys.Res.webServiceInvalidJsonWrapper, methodName));
}
result = result.d;
}
if (statusCode < 200 || statusCode >= 300 || errorObj)
{
var err;
if (result && errorObj)
{
err = result.get_exceptionType() + "-- " + result.get_message();
}
else
{
err = response.get_responseData();
}
throw Sys.Net.WebServiceProxy._createFailedError(methodName, String.format(Sys.Res.webServiceFailed, methodName, err));
}
}
else
{
var msg;
if (response.get_timedOut())
{
msg = String.format(Sys.Res.webServiceTimedOut, methodName);
}
else
{
msg = String.format(Sys.Res.webServiceFailedNoMsg, methodName);
}
throw Sys.Net.WebServiceProxy._createFailedError(methodName, msg);
}
}
return result;
};
System.Net.WebServiceSyncProxy.generateSyncProxy = function(proxy, proxyName, syncProxyName)
{
var e = Function._validateParams(arguments, [
{name: "proxy"},
{name: "proxyName", type: String},
{name: "syncProxyName", type: String, optional: true}
]);
if (e) { throw e; }
if (proxy && proxy.prototype)
{
if (!syncProxyName || 0 === syncProxyName.length)
{
syncProxyName = proxyName + "Sync";
}
var item;
var foundMethods = [];
for (var key in proxy.prototype)
{
if ("string" === typeof(key) && "_" !== key.charAt(0) && "constructor" !== key && !key.startsWith("get_") && !key.startsWith("set_"))
{
item = proxy.prototype[key];
if ("function" === typeof(item) && 3 <= item.length)
{
item = item.toString();
item = item.replace(/function[^\(]*\(/g, "function(");
item = item.replace(/\(succeededCallback,\s*failedCallback,\s*userContext\)/g, "()");
item = item.replace(/,\s*succeededCallback,\s*failedCallback,\s*userContext\)/g, ")");
item = item.replace("this._invoke(this._get_path(),", "System.Net.WebServiceSyncProxy.invokeSync(" + proxyName + ".get_path(),");
foundMethods.push(syncProxyName + "." + key + "=" + item);
}
}
}
if (0 !== foundMethods.length)
{
eval(syncProxyName + "=function(){if(arguments.length!==0){throw Error.parameterCount();}throw Error.notImplemented();};");
for (var i = 0; i < foundMethods.length; i++)
{
eval(foundMethods[i]);
}
}
}
};
System.Net.WebServiceSyncProxy.registerClass("System.Net.WebServiceSyncProxy", Sys.Net.WebServiceProxy);
Sys.Application.add_init(function()
{
if (window.PageMethods)
{
System.Net.WebServiceSyncProxy.generateSyncProxy(window.PageMethods, "PageMethods", "SyncPageMethods");
}
});
function ValidateForm() {
pageIsValid = true;
if (!SyncPageMethods.EmailAddressIsValid(document.getElementById('<%= txtNewMemberEmailAddress.ClientID %>').value))
{
document.getElementById('<%= imgNewMemberEmailAddress.ClientID %>').src = "/Images/cross.png";
emailAddressMessage = 'The email address you have entered is already in use';
pageIsValid = false;
}
else
{
document.getElementById('<%= imgNewMemberEmailAddress.ClientID %>').src = "/Images/tick.png";
emailAddressMessage = 'This field is valid';
}
...
return pageIsValid;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|