xinyb
2022-11-14 ec25390d182950eaab1c3facda467768a912578b
提交 | 用户 | age
a6a76f 1 /**
F 2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License.  You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 // AMQ Ajax Adapter for jQuery
20 // This class provides an adapter interface for the jquery library to perform
21 // some of the library-dependent tasks...namely logging and ajax.
22
23 var org = org || {};
24 org.activemq = org.activemq || {};
25
26 org.activemq.AmqAdapter = {
27
28     init: function(options) {
29     },
30
31     /**
32      *  Implement this method to make an AJAX call to the AjaxServlet. An
33      *  options object will accompany this class and will contain the properties
34      *  that describe the details of the AJAX call. The options object will
35      *  have the following properties:
36      *
37      *  - method:  'get' or 'post'
38      *  - data:    query data to accompany the post or get.
39      *  - success: A callback function that is invoked upon successful
40      *             completion of the AJAX call. The parameter is:
41      *             - data: The result of the AJAX call. In the case of XML
42      *                     data should resolve to a Document element.
43      *  - error:   A callback when some type of error occurs. The callback
44      *             function's parameters should be:
45      *             - xhr:    The XmlHttpRequest object.
46      *             - status: A text string of the status.
47      *             - ex:     The exception that caused the error.
48      *  - headers: An object containing additional headers for the ajax request.
49      */
50     ajax: function(uri, options) {
51         request = {
52             url: uri,
53             data: options.data,
54             success: options.success || function(){},
55             error: options.error || function(){}
56         }
57         var headers = {};
58         if( options.headers ) {
59             headers = options.headers;
60         }
61         
62         if (options.method == 'post') {
63             request.type = 'POST';
64             /* Force "Connection: close" for Mozilla browsers to work around
65              * a bug where XMLHttpReqeuest sends an incorrect Content-length
66              * header. See Mozilla Bugzilla #246651.
67              */
68             headers[ 'Connection' ] = 'close';
69         } else {
70             request.type = 'GET';
71             request.dataType = 'xml';
72         }
73         
74         if( headers ) {
75             request.beforeSend = function(xhr) {
76                 for( h in headers ) {
77                     xhr.setRequestHeader( h, headers[ h ] );
78                 }
79             }
80         }
81         
82         jQuery.ajax( request );
83     },
84
85     log: function(message, exception) {
86         if (typeof console != 'undefined' && console.log) console.log(message);
87     }
88 };