OpenLayers OpenLayers

Ticket #1565: request.patch

File request.patch, 47.8 kB (added by tschaub, 6 months ago)

add cross-browser XMLHttpRequest and convenience methods

  • build/license.txt

    old new  
    4343* 
    4444**/ 
    4545 
     46/** 
     47 * Contains XMLHttpRequest.js <http://code.google.com/p/xmlhttprequest/> 
     48 * Copyright 2007 Sergey Ilinsky (http://www.ilinsky.com) 
     49 * 
     50 * Licensed under the Apache License, Version 2.0 (the "License"); 
     51 * you may not use this file except in compliance with the License. 
     52 * You may obtain a copy of the License at 
     53 * http://www.apache.org/licenses/LICENSE-2.0 
     54 */ 
  • tests/Ajax.html

    old new  
    44  <script type="text/javascript"> 
    55 
    66    function test_Ajax_loadUrl(t) { 
    7         t.plan(1); 
    8         var req = OpenLayers.Ajax.Request; 
    9         OpenLayers.ProxyHost = "/?url="; 
    10         OpenLayers.Ajax.Request.prototype.request = function(uri) { 
    11             t.eq(uri, "/?url=http%3A%2F%2Fexample.com%2F%3Fformat%3Dimage%2Bkml", "URI matches what we expect from loadurl"); 
    12         } 
    13         OpenLayers.loadURL("http://example.com/?format=image+kml"); 
     7        t.plan(5); 
     8        var _get = OpenLayers.Request.GET; 
     9        var caller = {}; 
     10        var onComplete = function() {}; 
     11        var onFailure = function() {}; 
     12        var params = {}; 
     13        OpenLayers.Request.GET = function(config) { 
     14            t.eq(config.url, "http://example.com/?format=image+kml", "correct url") 
     15            t.eq(config.params, params, "correct params"); 
     16            t.eq(config.scope, caller, "correct scope"); 
     17            t.ok(config.success === onComplete, "correct success callback"); 
     18            t.ok(config.failure === onFailure, "correct failure callback"); 
     19        }; 
     20        OpenLayers.loadURL("http://example.com/?format=image+kml", params, caller, onComplete, onFailure); 
     21        OpenLayers.Request.GET = _get; 
    1422    } 
    1523  </script> 
    1624</head> 
  • tests/Request/XMLHttpRequest.html

    old new  
     1<html> 
     2<head> 
     3    <script src="../../lib/OpenLayers.js"></script> 
     4    <script type="text/javascript"> 
     5        function test_constructor(t) { 
     6            t.plan(1); 
     7            t.ok(new OpenLayers.Request.XMLHttpRequest(), 
     8                 "constructor didn't fail and we trust the code is well tested in OpenLayers.Request methods"); 
     9        } 
     10    </script> 
     11</head> 
     12<body> 
     13</body> 
     14</html> 
  • tests/Tile/WFS.html

    old new  
    6464 
    6565        var g_Success = {};         
    6666 
    67         var tLoadURL = OpenLayers.loadURL
    68         OpenLayers.loadURL = function(url, params, caller, onComplete) { 
    69             t.ok(url == tile.url, "tile's url correctly passed as 1st param to loadURL"); 
    70             t.ok(params == null, "null passed as 2nd param to loadURL"); 
    71             t.ok(caller == tile, "tile passed as 3rd param to loadURL"); 
    72             t.ok(onComplete == g_Success, "success param from loadFeaturesForRegion() passed as 4th param to loadURL"); 
     67        var _get = OpenLayers.Request.GET
     68        OpenLayers.Request.GET = function(config) { 
     69            t.ok(config.url == tile.url, "tile's url correctly passed"); 
     70            t.ok(config.params == null, "null params"); 
     71            t.ok(config.scope == tile, "tile passed as scope"); 
     72            t.ok(config.success == g_Success, "success passed"); 
    7373        }; 
    7474         
    7575      //no running request -- 4 tests 
     
    8282            } 
    8383        }; 
    8484        OpenLayers.Tile.WFS.prototype.loadFeaturesForRegion.apply(tile, [g_Success]); 
    85                  
    86         OpenLayers.loadURL = tLoadURL
     85 
     86        OpenLayers.Request.GET = _get
    8787    } 
    8888     
    8989    function test_Tile_WFS_destroy(t) { 
  • tests/Request.html

    old new  
     1<html> 
     2<head> 
     3    <script src="../lib/OpenLayers.js"></script> 
     4    <script type="text/javascript"> 
     5    function setup() { 
     6        window._xhr = OpenLayers.Request.XMLHttpRequest; 
     7        var anon = new Function(); 
     8        OpenLayers.Request.XMLHttpRequest = function() {}; 
     9        OpenLayers.Request.XMLHttpRequest.prototype = { 
     10            open: anon, 
     11            setRequestHeader: anon, 
     12            send: anon 
     13        }; 
     14        OpenLayers.Request.XMLHttpRequest.DONE = 4; 
     15    } 
     16    function teardown() { 
     17        OpenLayers.Request.XMLHttpRequest = window._xhr; 
     18        delete window._xhr; 
     19    } 
     20     
     21    function test_issue(t) { 
     22        setup(); 
     23 
     24        t.plan(18); 
     25        var request, config; 
     26        var proto = OpenLayers.Request.XMLHttpRequest.prototype; 
     27        var issue = OpenLayers.Function.bind(OpenLayers.Request.issue, 
     28                                             OpenLayers.Request); 
     29 
     30        // test that issue returns a new XMLHttpRequest - 1 test 
     31        request = issue(); 
     32        t.ok(request instanceof OpenLayers.Request.XMLHttpRequest, 
     33             "returns an XMLHttpRequest instance"); 
     34         
     35        // test that issue calls xhr.open with correct args from config - 5 tests 
     36        var _open = proto.open; 
     37        config = { 
     38            method: "foo", 
     39            url: "http://nowhere", 
     40            async: "bar", 
     41            user: "uncle", 
     42            password: "sam" 
     43        }; 
     44        proto.open = function(method, url, async, user, password) { 
     45            t.eq(method, config.method, "open called with correct method"); 
     46            t.eq(url, config.url, "open called with correct url"); 
     47            t.eq(async, config.async, "open called with correct async"); 
     48            t.eq(user, config.user, "open called with correct user"); 
     49            t.eq(password, config.password, "open called with correct password"); 
     50        } 
     51        request = issue(config); 
     52        proto.open = _open; 
     53         
     54        // test that headers are correctly set - 4 tests 
     55        var _setRequestHeader = proto.setRequestHeader; 
     56        config = { 
     57            headers: { 
     58                foo: "bar", 
     59                chicken: "soup" 
     60            } 
     61        }; 
     62        proto.setRequestHeader = function(key, value) { 
     63            t.ok(key in config.headers, "setRequestHeader called with key: " + key); 
     64            t.eq(value, config.headers[key], "setRequestHeader called with correct value: " + value); 
     65        } 
     66        request = issue(config); 
     67        proto.setRequestHeader = _setRequestHeader; 
     68         
     69        // test that callback is called (no scope) - 1 test 
     70        var unbound = function(request) { 
     71            t.ok(request instanceof OpenLayers.Request.XMLHttpRequest, 
     72                 "unbound callback called with xhr instance"); 
     73        } 
     74        config = { 
     75            callback: unbound 
     76        }; 
     77        request = issue(config); 
     78        request.readyState = OpenLayers.Request.XMLHttpRequest.DONE; 
     79        request.onreadystatechange(); 
     80 
     81        // test that callback is called (with scope) - 2 tests 
     82        var obj = {}; 
     83        var bound = function(request) { 
     84            t.ok(this === obj, "bound callback has correct scope"); 
     85            t.ok(request instanceof OpenLayers.Request.XMLHttpRequest, 
     86                 "bound callback called with xhr instance"); 
     87        } 
     88        config = { 
     89            callback: bound, 
     90            scope: obj 
     91        }; 
     92        request = issue(config); 
     93        request.readyState = 4; 
     94        request.onreadystatechange(); 
     95 
     96        // test that send is called with data - 1 test 
     97        var _send = proto.send; 
     98        config = { 
     99            method: "PUT", 
     100            data: "bling" 
     101        }; 
     102        proto.send = function(data) { 
     103            t.eq(data, config.data, "send called with correct data"); 
     104        } 
     105        request = issue(config); 
     106        proto.send = _send; 
     107         
     108        // test that optional success callback is only called with 200s and 
     109        // failure is only called with non-200s 
     110        var _send = proto.send; 
     111        proto.send = function() {}; 
     112         
     113        config = { 
     114            success: function(req) { 
     115                t.ok(req.status >= 200 && req.status < 300, 
     116                     "success callback called with " + req.status + " status"); 
     117            }, 
     118            failure: function(req) { 
     119                t.ok(req.status < 200 || req.status >= 300, 
     120                     "failure callback called with " + req.status + " status"); 
     121            } 
     122        }; 
     123        request = issue(config); 
     124        request.readyState = 4; 
     125         
     126        // mock up status 200 (1 test) 
     127        request.status = 200; 
     128        request.onreadystatechange(); 
     129         
     130        // mock up status 299 (1 test) 
     131        request.status = 299; 
     132        request.onreadystatechange(); 
     133         
     134        // mock up status 100 (1 test) 
     135        request.status = 100; 
     136        request.onreadystatechange(); 
     137 
     138        // mock up status 300 (1 test) 
     139        request.status = 300; 
     140        request.onreadystatechange(); 
     141 
     142        proto.send = _send; 
     143 
     144        teardown(); 
     145    } 
     146     
     147    function test_GET(t) { 
     148        t.plan(1); 
     149        var _issue = OpenLayers.Request.issue; 
     150        OpenLayers.Request.issue = function(config) { 
     151            t.eq(config.method, "GET", "calls issue with correct method"); 
     152        } 
     153        OpenLayers.Request.GET(); 
     154        OpenLayers.Request.issue = _issue; 
     155    } 
     156    function test_POST(t) { 
     157        t.plan(1); 
     158        var _issue = OpenLayers.Request.issue; 
     159        OpenLayers.Request.issue = function(config) { 
     160            t.eq(config.method, "POST", "calls issue with correct method"); 
     161        } 
     162        OpenLayers.Request.POST(); 
     163        OpenLayers.Request.issue = _issue; 
     164    } 
     165    function test_PUT(t) { 
     166        t.plan(1); 
     167        var _issue = OpenLayers.Request.issue; 
     168        OpenLayers.Request.issue = function(config) { 
     169            t.eq(config.method, "PUT", "calls issue with correct method"); 
     170        } 
     171        OpenLayers.Request.PUT(); 
     172        OpenLayers.Request.issue = _issue; 
     173    } 
     174    function test_DELETE(t) { 
     175        t.plan(1); 
     176        var _issue = OpenLayers.Request.issue; 
     177        OpenLayers.Request.issue = function(config) { 
     178            t.eq(config.method, "DELETE", "calls issue with correct method"); 
     179        } 
     180        OpenLayers.Request.DELETE(); 
     181        OpenLayers.Request.issue = _issue; 
     182    } 
     183    function test_HEAD(t) { 
     184        t.plan(1); 
     185        var _issue = OpenLayers.Request.issue; 
     186        OpenLayers.Request.issue = function(config) { 
     187            t.eq(config.method, "HEAD", "calls issue with correct method"); 
     188        } 
     189        OpenLayers.Request.HEAD(); 
     190        OpenLayers.Request.issue = _issue; 
     191    } 
     192    function test_OPTIONS(t) { 
     193        t.plan(1); 
     194        var _issue = OpenLayers.Request.issue; 
     195        OpenLayers.Request.issue = function(config) { 
     196            t.eq(config.method, "OPTIONS", "calls issue with correct method"); 
     197        } 
     198        OpenLayers.Request.OPTIONS(); 
     199        OpenLayers.Request.issue = _issue; 
     200    } 
     201    </script> 
     202</head> 
     203<body> 
     204</body> 
     205</html> 
  • tests/manual/ajax.html

    old new  
    11<html xmlns="http://www.w3.org/1999/xhtml"> 
    22  <head> 
    3     <title>Ajax Acceptance Test</title> 
    4     <style type="text/css"> 
    5      
    6         body { 
    7             font-size: 0.8em; 
    8         } 
    9         p { 
    10             padding-top: 1em; 
    11         } 
    12          
    13         .buttons { 
    14             margin: 1em; 
    15             float: left; 
    16         } 
    17  
    18     </style> 
    19  
     3    <title>XHR Acceptance Test</title> 
    204    <script src="../../lib/OpenLayers.js"></script> 
    215    <script type="text/javascript"> 
    226        var url = "ajax.txt"; 
    237        function sendSynchronous(){ 
    24             var request = new OpenLayers.Ajax.Request(url, { 
    25                'asynchronous': false, 
    26                 onComplete: function() { 
     8            var request = OpenLayers.Request.GET({ 
     9                url: url, 
     10                async: false, 
     11                callback: function() { 
    2712                    document.getElementById('send_sync').value += 'request completed\n'; 
    2813                } 
    2914            }); 
    3015            document.getElementById('send_sync').value += 'other processing\n'; 
    3116        } 
    3217        function sendAsynchronous(){ 
    33             var request = new OpenLayers.Ajax.Request(url, { 
    34                 onComplete: function() { 
     18            var request = OpenLayers.Request.GET({ 
     19                url: url, 
     20                callback: function() { 
    3521                    document.getElementById('send_sync').value += 'request completed\n'; 
    3622                } 
    3723            }); 
    3824            document.getElementById('send_sync').value += 'other processing\n'; 
    3925        } 
    4026        function sendAndAbort(){ 
    41             var request = new OpenLayers.Ajax.Request(url, { 
    42                 onComplete: function(request) { 
    43                     if (request.responseText == '') { 
    44                         document.getElementById('send_sync').value += 'request aborted\n'; 
    45                     } 
     27            var request = OpenLayers.Request.GET({ 
     28                url: url, 
     29                callback: function() { 
     30                    document.getElementById('send_sync').value += 'never called\n'; 
    4631                } 
    4732            }); 
    48             request.transport.abort(); 
     33            request.abort(); 
    4934            document.getElementById('send_sync').value += 'other processing\n'; 
    5035        } 
    5136 
    52     </script> 
    53   </head> 
    54   <body > 
    55     <div class="buttons"> 
    56         <button onclick="sendSynchronous()">Send an synchronous Ajax request</button><br /> 
    57         <button onclick="sendAsynchronous()">Send an asynchronous Ajax request</button><br /> 
    58         <button onclick="sendAndAbort()">Send a request and abort it</button><br /> 
     37        </script> 
     38    </head> 
     39    <body > 
     40        <button onclick="sendSynchronous()">synchronous</button> 
     41        expected output: "request completed" then "other processing"<br /> 
     42        <button onclick="sendAsynchronous()">asynchronous</button> 
     43        expected output: "other processing" then "request completed"<br /> 
     44        <button onclick="sendAndAbort()">send and abort</button> 
     45        expected output: "other processing" (and not "never called")<br /> 
    5946        <textarea id="send_sync" rows="6"></textarea><br /> 
    6047        <button onclick="document.getElementById('send_sync').value = ''">Clear</button> 
    61     </div> 
    62     <p><b></b></p> 
    63     <p>Clicking on the different buttons should give the following results in the textarea below :</p> 
    64     <ul> 
    65       <li>synchronous: "request completed" then "other processing"</li> 
    66       <li>asynchronous: "other processing" then "request completed"</li> 
    67       <li>abort: "request aborted" then "other processing" (note that real XHR behavior would not call onComplete with abort - meaning "request aborted" would not be displayed here)</li> 
    68     </ul> 
    6948  </body> 
    7049</html> 
  • tests/list-tests.html

    old new  
    5757    <li>Lang.html</li> 
    5858    <li>Layer.html</li> 
    5959    <li>Renderer.html</li> 
     60    <li>Request.html</li> 
     61    <li>Request/XMLHttpRequest.html</li> 
    6062    <li>Layer/EventPane.html</li> 
    6163    <li>Layer/FixedZoomLevels.html</li> 
    6264    <li>Layer/GeoRSS.html</li> 
  • lib/OpenLayers/Format/KML.js

    old new  
    249249     *  
    250250     */ 
    251251    fetchLink: function(href) { 
    252         var request = new OpenLayers.Ajax.Request(href,  
    253                       {method: 'get', asynchronous: false }); 
    254  
    255         if (request && request.transport) { 
    256             return request.transport.responseText; 
     252        var request = OpenLayers.Request.GET({url: href, async: false}); 
     253        if (request) { 
     254            return request.responseText; 
    257255        } 
    258256    }, 
    259257 
  • lib/OpenLayers/Request/XMLHttpRequest.js

    old new  
     1// Copyright 2007 Sergey Ilinsky (http://www.ilinsky.com) 
     2// 
     3// Licensed under the Apache License, Version 2.0 (the "License"); 
     4// you may not use this file except in compliance with the License. 
     5// You may obtain a copy of the License at 
     6// 
     7//   http://www.apache.org/licenses/LICENSE-2.0 
     8// 
     9// Unless required by applicable law or agreed to in writing, software 
     10// distributed under the License is distributed on an "AS IS" BASIS, 
     11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
     12// See the License for the specific language governing permissions and 
     13// limitations under the License. 
     14 
     15/** 
     16 * @requires OpenLayers/Request.js 
     17 */ 
     18 
     19(function () { 
     20 
     21    // Save reference to earlier defined object implementation (if any) 
     22    var oXMLHttpRequest    = window.XMLHttpRequest; 
     23 
     24    // Define on browser type 
     25    var bGecko    = !!window.controllers, 
     26        bIE        = window.document.all && !window.opera; 
     27 
     28    // Constructor 
     29    function cXMLHttpRequest() { 
     30        this._object    = oXMLHttpRequest ? new oXMLHttpRequest : new window.ActiveXObject('Microsoft.XMLHTTP'); 
     31    }; 
     32 
     33    // BUGFIX: Firefox with Firebug installed would break pages if not executed 
     34    if (bGecko && oXMLHttpRequest.wrapped) 
     35        cXMLHttpRequest.wrapped    = oXMLHttpRequest.wrapped; 
     36 
     37    // Constants 
     38    cXMLHttpRequest.UNSENT                = 0; 
     39    cXMLHttpRequest.OPENED                = 1; 
     40    cXMLHttpRequest.HEADERS_RECEIVED    = 2; 
     41    cXMLHttpRequest.LOADING                = 3; 
     42    cXMLHttpRequest.DONE                = 4; 
     43 
     44    // Public Properties 
     45    cXMLHttpRequest.prototype.readyState    = cXMLHttpRequest.UNSENT; 
     46    cXMLHttpRequest.prototype.responseText    = ""; 
     47    cXMLHttpRequest.prototype.responseXML    = null; 
     48    cXMLHttpRequest.prototype.status        = 0; 
     49    cXMLHttpRequest.prototype.statusText    = ""; 
     50 
     51    // Instance-level Events Handlers 
     52    cXMLHttpRequest.prototype.onreadystatechange    = null; 
     53 
     54    // Class-level Events Handlers 
     55    cXMLHttpRequest.onreadystatechange    = null; 
     56    cXMLHttpRequest.onopen                = null; 
     57    cXMLHttpRequest.onsend                = null; 
     58    cXMLHttpRequest.onabort                = null; 
     59 
     60    // Public Methods 
     61    cXMLHttpRequest.prototype.open    = function(sMethod, sUrl, bAsync, sUser, sPassword) { 
     62 
     63        // Save async parameter for fixing Gecko bug with missing readystatechange in synchronous requests 
     64        this._async        = bAsync; 
     65 
     66        // Set the onreadystatechange handler 
     67        var oRequest    = this, 
     68            nState        = this.readyState; 
     69 
     70        // BUGFIX: IE - memory leak on page unload (inter-page leak) 
     71        if (bIE) { 
     72            var fOnUnload    = function() { 
     73                if (oRequest._object.readyState != cXMLHttpRequest.DONE) 
     74                    fCleanTransport(oRequest); 
     75            }; 
     76            if (bAsync) 
     77                window.attachEvent("onunload", fOnUnload); 
     78        } 
     79 
     80        this._object.onreadystatechange    = function() { 
     81            if (bGecko && !bAsync) 
     82                return; 
     83 
     84            // Synchronize state 
     85            oRequest.readyState        = oRequest._object.readyState; 
     86 
     87            // 
     88            fSynchronizeValues(oRequest); 
     89 
     90            // BUGFIX: Firefox fires unneccesary DONE when aborting 
     91            if (oRequest._aborted) { 
     92                // Reset readyState to UNSENT 
     93                oRequest.readyState    = cXMLHttpRequest.UNSENT; 
     94 
     95                // Return now 
     96                return; 
     97            } 
     98 
     99            if (oRequest.readyState == cXMLHttpRequest.DONE) { 
     100                // 
     101                fCleanTransport(oRequest); 
     102// Uncomment this block if you need a fix for IE cache 
     103/* 
     104                // BUGFIX: IE - cache issue 
     105                if (!oRequest._object.getResponseHeader("Date")) { 
     106                    // Save object to cache 
     107                    oRequest._cached    = oRequest._object; 
     108 
     109                    // Instantiate a new transport object 
     110                    cXMLHttpRequest.call(oRequest); 
     111 
     112                    // Re-send request 
     113                    oRequest._object.open(sMethod, sUrl, bAsync, sUser, sPassword); 
     114                    oRequest._object.setRequestHeader("If-Modified-Since", oRequest._cached.getResponseHeader("Last-Modified") || new window.Date(0)); 
     115                    // Copy headers set 
     116                    if (oRequest._headers) 
     117                        for (var sHeader in oRequest._headers) 
     118                            if (typeof oRequest._headers[sHeader] == "string")    // Some frameworks prototype objects with functions 
     119                                oRequest._object.setRequestHeader(sHeader, oRequest._headers[sHeader]); 
     120 
     121                    oRequest._object.onreadystatechange    = function() { 
     122                        // Synchronize state 
     123                        oRequest.readyState        = oRequest._object.readyState; 
     124 
     125                        if (oRequest._aborted) { 
     126                            // 
     127                            oRequest.readyState    = cXMLHttpRequest.UNSENT; 
     128 
     129                            // Return 
     130                            return; 
     131                        } 
     132 
     133                        if (oRequest.readyState == cXMLHttpRequest.DONE) { 
     134                            // Clean Object 
     135                            fCleanTransport(oRequest); 
     136 
     137                            // get cached request 
     138                            if (oRequest.status == 304) 
     139                                oRequest._object    = oRequest._cached; 
     140 
     141                            // 
     142                            delete oRequest._cached; 
     143 
     144                            // 
     145                            fSynchronizeValues(oRequest); 
     146 
     147                            // 
     148                            fReadyStateChange(oRequest); 
     149 
     150                            // BUGFIX: IE - memory leak in interrupted 
     151                            if (bIE && bAsync) 
     152                                window.detachEvent("onunload", fOnUnload); 
     153                        } 
     154                    }; 
     155                    oRequest._object.send(null); 
     156 
     157                    // Return now - wait untill re-sent request is finished 
     158                    return; 
     159                }; 
     160*/ 
     161                // BUGFIX: IE - memory leak in interrupted 
     162                if (bIE && bAsync) 
     163                    window.detachEvent("onunload", fOnUnload); 
     164            } 
     165 
     166            // BUGFIX: Some browsers (Internet Explorer, Gecko) fire OPEN readystate twice 
     167            if (nState != oRequest.readyState) 
     168                fReadyStateChange(oRequest); 
     169 
     170            nState    = oRequest.readyState; 
     171        }; 
     172 
     173        // Add method sniffer 
     174        if (cXMLHttpRequest.onopen) 
     175            cXMLHttpRequest.onopen.apply(this, arguments); 
     176 
     177        this._object.open(sMethod, sUrl, bAsync, sUser, sPassword); 
     178 
     179        // BUGFIX: Gecko - missing readystatechange calls in synchronous requests 
     180        if (!bAsync && bGecko) { 
     181            this.readyState    = cXMLHttpRequest.OPENED; 
     182 
     183            fReadyStateChange(this); 
     184        } 
     185    }; 
     186    cXMLHttpRequest.prototype.send    = function(vData) { 
     187        // Add method sniffer 
     188        if (cXMLHttpRequest.onsend) 
     189            cXMLHttpRequest.onsend.apply(this, arguments); 
     190 
     191        // BUGFIX: Safari - fails sending documents created/modified dynamically, so an explicit serialization required 
     192        // BUGFIX: IE - rewrites any custom mime-type to "text/xml" in case an XMLNode is sent 
     193        // BUGFIX: Gecko - fails sending Element (this is up to the implementation either to standard) 
     194        if (vData && vData.nodeType) { 
     195            vData    = window.XMLSerializer ? new window.XMLSerializer().serializeToString(vData) : vData.xml; 
     196            if (!this._headers["Content-Type"]) 
     197                this._object.setRequestHeader("Content-Type", "application/xml"); 
     198        } 
     199 
     200        this._object.send(vData); 
     201 
     202        // BUGFIX: Gecko - missing readystatechange calls in synchronous requests 
     203        if (bGecko && !this._async) { 
     204            this.readyState    = cXMLHttpRequest.OPENED; 
     205 
     206            // Synchronize state 
     207            fSynchronizeValues(this); 
     208 
     209            // Simulate missing states 
     210            while (this.readyState < cXMLHttpRequest.DONE) { 
     211                this.readyState++; 
     212                fReadyStateChange(this); 
     213                // Check if we are aborted 
     214                if (this._aborted) 
     215                    return; 
     216            } 
     217        } 
     218    }; 
     219    cXMLHttpRequest.prototype.abort    = function() { 
     220        // Add method sniffer 
     221        if (cXMLHttpRequest.onabort) 
     222            cXMLHttpRequest.onabort.apply(this, arguments); 
     223 
     224        // BUGFIX: Gecko - unneccesary DONE when aborting 
     225        if (this.readyState > cXMLHttpRequest.UNSENT) 
     226            this._aborted    = true; 
     227 
     228        this._object.abort(); 
     229 
     230        // BUGFIX: IE - memory leak 
     231        fCleanTransport(this); 
     232    }; 
     233    cXMLHttpRequest.prototype.getAllResponseHeaders    = function() { 
     234        return this._object.getAllResponseHeaders(); 
     235    }; 
     236    cXMLHttpRequest.prototype.getResponseHeader    = function(sName) { 
     237        return this._object.getResponseHeader(sName); 
     238    }; 
     239    cXMLHttpRequest.prototype.setRequestHeader    = function(sName, sValue) { 
     240        // BUGFIX: IE - cache issue 
     241        if (!this._headers) 
     242            this._headers    = {}; 
     243        this._headers[sName]    = sValue; 
     244 
     245        return this._object.setRequestHeader(sName, sValue); 
     246    }; 
     247    cXMLHttpRequest.prototype.toString    = function() { 
     248        return '[' + "object" + ' ' + "XMLHttpRequest" + ']'; 
     249    }; 
     250    cXMLHttpRequest.toString    = function() { 
     251        return '[' + "XMLHttpRequest" + ']'; 
     252    }; 
     253 
     254    // Helper function 
     255    function fReadyStateChange(oRequest) { 
     256        // Execute onreadystatechange 
     257        if (oRequest.onreadystatechange) 
     258            oRequest.onreadystatechange.apply(oRequest); 
     259 
     260        // Sniffing code 
     261        if (cXMLHttpRequest.onreadystatechange) 
     262            cXMLHttpRequest.onreadystatechange.apply(oRequest); 
     263    }; 
     264 
     265    function fGetDocument(oRequest) { 
     266        var oDocument    = oRequest.responseXML; 
     267        // Try parsing responseText 
     268        if (bIE && oDocument && !oDocument.documentElement && oRequest.getResponseHeader("Content-Type").match(/[^\/]+\/[^\+]+\+xml/)) { 
     269            oDocument    = new ActiveXObject('Microsoft.XMLDOM'); 
     270            oDocument.loadXML(oRequest.responseText); 
     271        } 
     272        // Check if there is no error in document 
     273        if (oDocument) 
     274            if ((bIE && oDocument.parseError != 0) || (oDocument.documentElement && oDocument.documentElement.tagName == "parsererror")) 
     275                return null; 
     276        return oDocument; 
     277    }; 
     278 
     279    function fSynchronizeValues(oRequest) { 
     280        try {    oRequest.responseText    = oRequest._object.responseText;    } catch (e) {} 
     281        try {    oRequest.responseXML    = fGetDocument(oRequest._object);    } catch (e) {} 
     282        try {    oRequest.status            = oRequest._object.status;            } catch (e) {} 
     283        try {    oRequest.statusText        = oRequest._object.statusText;        } catch (e) {} 
     284    }; 
     285 
     286    function fCleanTransport(oRequest) { 
     287        // BUGFIX: IE - memory leak (on-page leak) 
     288        oRequest._object.onreadystatechange    = new window.Function; 
     289 
     290        // Delete private properties 
     291        delete oRequest._headers; 
     292    }; 
     293 
     294    // Internet Explorer 5.0 (missing apply) 
     295    if (!window.Function.prototype.apply) { 
     296        window.Function.prototype.apply    = function(oRequest, oArguments) { 
     297            if (!oArguments) 
     298                oArguments    = []; 
     299            oRequest.__func    = this; 
     300            oRequest.__func(oArguments[0], oArguments[1], oArguments[2], oArguments[3], oArguments[4]); 
     301            delete oRequest.__func; 
     302        }; 
     303    }; 
     304 
     305    // Register new object with window 
     306    /** 
     307     * Class: OpenLayers.Request.XMLHttpRequest 
     308     * Standard-compliant (W3C) cross-browser implementation of the 
     309     *     XMLHttpRequest object.  From 
     310     *     http://code.google.com/p/xmlhttprequest/. 
     311     */ 
     312    OpenLayers.Request.XMLHttpRequest = cXMLHttpRequest; 
     313})(); 
  • lib/OpenLayers/Tile/WFS.js

    old new  
    3232     
    3333    /**  
    3434     * Property: request  
    35      * {OpenLayers.Ajax.Request}  
     35     * {<OpenLayers.Request.XMLHttpRequest>}  
    3636     */  
    3737    request: null,      
    3838     
     
    100100 
    101101    /**  
    102102    * Method: loadFeaturesForRegion 
    103     * get the full request string from the ds and the tile params  
    104     *     and call the AJAX loadURL().  
     103    * Abort any pending requests and issue another request for data.  
    105104    * 
    106105    * Input are function pointers for what to do on success and failure. 
    107106    * 
     
    113112        if(this.request) { 
    114113            this.request.abort(); 
    115114        } 
    116         this.request = OpenLayers.loadURL(this.url, null, this, success); 
     115        this.request = OpenLayers.Request.GET({ 
     116            url: this.url, 
     117            success: success, 
     118            failure: failure, 
     119            scope: this 
     120        }); 
    117121    }, 
    118122     
    119123    /** 
     
    122126    * layer.addFeatures in vector mode, addResults otherwise.  
    123127    * 
    124128    * Parameters: 
    125     * request - {XMLHttpRequest
     129    * request - {<OpenLayers.Request.XMLHttpRequest>
    126130    */ 
    127131    requestSuccess:function(request) { 
    128132        if (this.features) { 
  • lib/OpenLayers/Ajax.js

    old new  
    3030 */ 
    3131 
    3232 
    33 /**  
    34 * @param {} request 
    35 */ 
     33/** 
     34 * Function: OpenLayers.nullHandler 
     35 * @param {} request 
     36 */ 
    3637OpenLayers.nullHandler = function(request) { 
    3738    alert(OpenLayers.i18n("unhandledRequest", {'statusText':request.statusText})); 
    3839}; 
    3940 
    4041/**  
    4142 * Function: loadURL 
    42  * Background load a document. 
     43 * Background load a document.  For more flexibility in using XMLHttpRequest, 
     44 *     see the <OpenLayers.Request> methods. 
    4345 * 
    4446 * Parameters: 
    4547 * uri - {String} URI of source doc 
     
    4951 * caller - {Object} object which gets callbacks 
    5052 * onComplete - {Function} Optional callback for success.  The callback 
    5153 *     will be called with this set to caller and will receive the request 
    52  *     object as an argument. 
     54 *     object as an argument.  Note that if you do not specify an onComplete 
     55 *     function, <OpenLayers.nullHandler> will be called (which pops up an 
     56 *     alert dialog). 
    5357 * onFailure - {Function} Optional callback for failure.  In the event of 
    5458 *     a failure, the callback will be called with this set to caller and will 
    55  *     receive the request object as an argument. 
     59 *     receive the request object as an argument.  Note that if you do not 
     60 *     specify an onComplete function, <OpenLayers.nullHandler> will be called 
     61 *     (which pops up an alert dialog). 
    5662 * 
    5763 * Returns: 
    58  * {XMLHttpRequest}  The request object.  To abort loading, call 
    59  *     request.abort(). 
     64 * {<OpenLayers.Request.XMLHttpRequest>}  The request object. To abort loading, 
     65 *     call request.abort(). 
    6066 */ 
    6167OpenLayers.loadURL = function(uri, params, caller, 
    6268                                  onComplete, onFailure) { 
    63  
    64     var success = (onComplete) ? OpenLayers.Function.bind(onComplete, caller) 
    65                                 : OpenLayers.nullHandler; 
    66  
    67     var failure = (onFailure) ? OpenLayers.Function.bind(onFailure, caller) 
    68                            : OpenLayers.nullHandler; 
    69  
    70     // from prototype.js 
    71     var request = new OpenLayers.Ajax.Request( 
    72         uri,  
    73         { 
    74             method: 'get',  
    75             parameters: params, 
    76             onComplete: success,  
    77             onFailure: failure 
    78         } 
    79     ); 
    80     return request.transport; 
     69     
     70    if(typeof params == 'string') { 
     71        params = OpenLayers.Util.getParameters(params); 
     72    } 
     73    var success = (onComplete) ? onComplete : OpenLayers.nullHandler; 
     74    var failure = (onFailure) ? onFailure : OpenLayers.nullHandler; 
     75     
     76    return OpenLayers.Request.GET({ 
     77        url: uri, params: params, 
     78        success: success, failure: failure, scope: caller 
     79    }); 
    8180}; 
    8281 
    8382/**  
     
    263262 
    264263/** 
    265264 * Class: OpenLayers.Ajax.Request 
     265 * *Deprecated*.  Use <OpenLayers.Request> method instead. 
    266266 * 
    267267 * Inherit: 
    268268 *  - <OpenLayers.Ajax.Base> 
  • lib/OpenLayers/Request.js

    <
    old new  
     1/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD 
     2 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the 
     3 * full text of the license. */ 
     4 
     5/** 
     6 * Namespace: OpenLayers.Request 
     7 * The OpenLayers.Request namespace contains convenience methods for working 
     8 *     with XMLHttpRequests.  These methods work with a cross-browser 
     9 *     W3C compliant <OpenLayers.Request.XMLHttpRequest> class. 
     10 */ 
     11OpenLayers.Request = { 
     12     
     13    /** 
     14     * Constant: DEFAULT_CONFIG 
     15     * {Object} Default configuration for all requests. 
     16     */ 
     17    DEFAULT_CONFIG: { 
     18        method: "GET", 
     19        url: window.location.href, 
     20        async: true, 
     21        user: undefined, 
     22        password: undefined, 
     23        params: null, 
     24        proxy: OpenLayers.ProxyHost, 
     25        headers: {}, 
     26        data: null, 
     27        callback: function() {}, 
     28        success: null, 
     29        failure: null, 
     30        scope: null 
     31    }, 
     32     
     33    /** 
     34     * APIMethod: issue 
     35     * Create a new XMLHttpRequest object, open it, set any headers, bind 
     36     *     a callback to done state, and send any data. 
     37     * 
     38     * Parameters: 
     39     * config - {Object} Object containing properties for configuring the 
     40     *     request.  Allowed configuration properties are described below. 
     41     * 
     42     * Allowed config properties: 
     43     * method - {String} One of GET, POST, PUT, DELETE, HEAD, or 
     44     *     OPTIONS.  Default is GET. 
     45     * url - {String} URL for the request. 
     46     * async - {Boolean} Open an asynchronous request.  Default is true. 
     47     * user - {String} User for relevant authentication scheme.  Set 
     48     *     to null to clear current user. 
     49     * password - {String} Password for relevant authentication scheme. 
     50     *     Set to null to clear current password. 
     51     * proxy - {String} Optional proxy.  Defaults to 
     52     *     <OpenLayers.ProxyHost>. 
     53     * params - {Object} Any key:value pairs to be appended to the 
     54     *     url as a query string.  Assumes url doesn't already include a query 
     55     *     string or hash.  Parameter values that are arrays will be 
     56     *     concatenated with a comma (note that this goes against form-encoding) 
     57     *     as is done with <OpenLayers.Util.getParameterString>. 
     58     * headers - {Object} Object with header:value pairs to be set on 
     59     *     the request. 
     60     * data - {Object} Any data to send with the request. 
     61     * callback - {Function} Function to call when request is done. 
     62     *     To determine if the request failed, check request.status (200 
     63     *     indicates success). 
     64     * success - {Function} Optional function to call if request status is in 
     65     *     the 200s.  This will be called in addition to callback above and 
     66     *     would typically only be used as an alternative. 
     67     * failure - {Function} Optional function to call if request status is not 
     68     *     in the 200s.  This will be called in addition to callback above and 
     69     *     would typically only be used as an alternative. 
     70     * scope - {Object} If callback is a public method on some object, 
     71     *     set the scope to that object. 
     72     * 
     73     * Returns: 
     74     * {XMLHttpRequest} Request object.  To abort the request before a response 
     75     *     is received, call abort() on the request object. 
     76     */ 
     77    issue: function(config) {         
     78        // apply default config - proxy host may have changed 
     79        var defaultConfig = OpenLayers.Util.extend( 
     80            this.DEFAULT_CONFIG, 
     81            {proxy: OpenLayers.ProxyHost} 
     82        ); 
     83        config = OpenLayers.Util.applyDefaults(config, defaultConfig); 
     84 
     85        // create request, open, and set headers 
     86        var request = new OpenLayers.Request.XMLHttpRequest(); 
     87        var url = config.url; 
     88        if(config.params) { 
     89            url += "?" + OpenLayers.Util.getParameterString(config.params); 
     90        } 
     91        if(config.proxy && (url.indexOf("http") == 0)) { 
     92            url = config.proxy + encodeURIComponent(url); 
     93        } 
     94        request.open( 
     95            config.method, url, config.async, config.user, config.password 
     96        ); 
     97        for(var header in config.headers) { 
     98            request.setRequestHeader(header, config.headers[header]); 
     99        } 
     100 
     101        // bind callbacks to readyState 4 (done) 
     102        var complete = (config.scope) ? 
     103            OpenLayers.Function.bind(config.callback, config.scope) : 
     104            config.callback; 
     105         
     106        // optional success callback 
     107        var success; 
     108        if(config.success) { 
     109            success = (config.scope) ? 
     110                OpenLayers.Function.bind(config.success, config.scope) : 
     111                config.success;