Listing The onComplete Private JavaScript Function

function onComplete(response, eventArgs) {

if (response.get_responseAvailable())

var result = null;

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();

var error = response.getResponseHeader("jsonerror"); var errorObj = (error === "true"); if (errorObj)

result = new Sys.Net.WebServiceError(false, result.Message, result.StackTrace, result.ExceptionType);

var statusCode = response.get_statusCode();

if (((statusCode < 200) || (statusCode >= 300)) || errorObj) {

result = new Sys.Net.WebServiceError(false /*timedout*/,

String.format(Sys.Res.webServiceFailedNoMsg, methodName), "", "");

result._statusCode = statusCode; onFailure(result, userContext, methodName);

var error;

if (result && errorObj)

error = result.get_exceptionType() + "-- " + result.get_message(); else error = response.get_responseData(); alert(String.format(Sys.Res.webServiceFailed, methodName, error));

else if (onSuccess)

onSuccess(result, userContext, methodName);

var msg;

if (response.get_timedOut())

msg = String.format(Sys.Res.webServiceTimedOut, methodName);

i lse msg = String.format(Sys.Res.webServiceFailedNoMsg, methodName)

if (onFailure) onFailure(

new Sys.Net.WebServiceError(response.get_timedOut(), msg, "", ""), userContext, methodName);

else alert(msg);

When this function is invoked, two parameters are passed into it. The first parameter references the WebRequestExecutor object responsible for executing the current request. As discussed in the previous chapters, the completion of a request does not automatically mean that everything went fine and the server response has successfully arrived. Because the completed event could be raised for a number of reasons, the onComplete method must first determine what caused the completed event to fire (as does any method registered for a WebRequest object's completed event). The boldface portions of Listing 14-10 contain the code that makes this determination.

As you can see in the following excerpt from Listing 14-10, if the request has completed because something went wrong (for example, because the request timed out), the onComplete function invokes the failure JavaScript function if such a function has been specified. Otherwise, it simply calls the alert function to display the error massage in a pop-up box.

iar msg;

if (response.get_timedOut()) msg = String.format(Sys.Res.webServiceTimedOut, methodName);

else msg = String.format(Sys.Res.webServiceFailedNoMsg, methodName)

if (onFailure)

onFailure(

new Sys.Net.WebServiceError(response.get_timedOut()

, msg, "", ""),

userContext, methodName);

else

alert(msg);

If the request has completed because the server response has successfully arrived, the onComplete function performs the following tasks:

1. It invokes the getResponseHeader method on the WebRequestExecutor object responsible for executing the current request to return the value of the response header named Content-Type:

var contentType = response.getResponseHeader("Content-Type");

2. If the value of the Content-Type response header starts with the string "application/json", the response contains a JSON object and, consequently, the onComplete method invokes the get_object method on the WebRequestExecutor object to access this JSON object, and stores the object in a local variable named result:

if (contentType.startsWith("application/json")) result = response.get_object();

3. If the value of the Content-Type response header starts with the string "text/xml", the response contains an XML document and, consequently, the onComplete method calls the get_xml method on the WebRequestExecutor object to access this XML document, and stores this document in the result local variable:

else if (contentType.startsWith("text/xml")) result = response.get_xml();

4. If the value of the Content-Type response header does not start with either the "application/json" string or the "text/xml" string, the onComplete method calls the get_responseData method on the WebRequestExecutor object to access the server response, and stores it in the result local variable:

else result = response.get_responseData();

Next, the onComplete method calls the getResponseHeader method on the WebRequestExecutor object to return the value of a response header named jsonerror:

var error = response.getResponseHeader("jsonerror");

If the server response contains this response header, and if the value of this header is the string "true", the response contains information about an error that occurred when the server was processing the current request. The server response is stored in the result local variable, and the server uses the jsonerror custom HTTP header to signal the onComplete method that the response contains information about an error. This information includes the error message, stack trace, and exception type. As the following excerpt from Listing 14-10 shows, the onComplete method creates an instance of a class named Sys.Net.WebServiceError, passing in the error message, stack trace, and exception type, and stores this instance in the result local variable:

var errorObj = (error === "true");

if (errorObj)

result = new Sys.Net.WebServiceError(false,

result.Message,

result.

.StackTrace,

result.

. ExceptionType);

Next, the invoke method calls the get_statusCode method on the WebRequestExecutor object to return the HTTP status code of the server response:

var statusCode = response.get_statusCode(); The method then checks whether at least one of the following conditions is met:

□ The HTTP status code is less than 200, or greater than or equal to 300, which indicates that a server error has occurred.

□ The server response contains a response header named jsonerror with a value of true.

Either of these conditions indicates a server error and, consequently, the invoke method takes the following steps to report the error:

1. If the onFailure parameter is not null, it means the caller of the invoke method has specified a JavaScript function as the value of this parameter. Consequently, the invoke method sets the _statusCode property of the result local variable to the server response's HTTP status code, and invokes the JavaScript function referenced by the onFailure parameter, passing in three parameters. The first parameter references the result local variable, the second parameter references the user context object, and the third parameter references the name of the Web method that was invoked:

result._statusCode = statusCode; onFailure(result, userContext, methodName);

2. If the caller of the invoke method has not specified a value for the onFailure parameter, the method invokes the alert function to display the error message in a pop-up box:

alert(String.format(Sys.Res.webServiceFailed, methodName, error));

3. If the server response HTTP status code is a number equal to or greater than 200 but less than 300, this indicates that everything has gone fine on the server side and, consequently, the invoke method invokes the JavaScript function referenced by the onSuccess parameter (if any), passing in three parameters. The first parameter references the result local variable, the second parameter references the user context, and the third parameter contains the name of the Web method invoked:

else if (onSuccess)

onSuccess(result, userContext, methodName);

0 0

Post a comment

  • Receive news updates via email from this site