root/trunk/httpreq.js

Revision 137, 2.9 kB (checked in by mischa, 3 years ago)

better error message

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1var HTTPReq = new Object;
2
3HTTPReq.create = function () {
4    var xtr;
5    var ex;
6
7    if (typeof(XMLHttpRequest) != "undefined") {
8        xtr = new XMLHttpRequest();
9    } else {
10        try {
11            xtr = new ActiveXObject("Msxml2.XMLHTTP.4.0");
12        } catch (ex) {
13            try {
14                xtr = new ActiveXObject("Msxml2.XMLHTTP");
15            } catch (ex) {
16            }
17        }
18    }
19
20    // let me explain this.  Opera 8 does XMLHttpRequest, but not setRequestHeader.
21    // no problem, we thought:  we'll test for setRequestHeader and if it's not present
22    // then fall back to the old behavior (treat it as not working).  BUT --- IE6 won't
23    // let you even test for setRequestHeader without throwing an exception (you need
24    // to call .open on the .xtr first or something)
25    try {
26        if (xtr && ! xtr.setRequestHeader)
27            xtr = null;
28    } catch (ex) { }
29
30    return xtr;
31};
32
33// opts:
34// url, onError, onData, method (GET or POST), data
35// url: where to get/post to
36// onError: callback on error
37// onData: callback on data received
38// method: HTTP method, GET by default
39// data: what to send to the server (urlencoded)
40HTTPReq.getJSON = function (opts) {
41    var req = HTTPReq.create();
42    if (! req) {
43        if (opts.onError) opts.onError("noxmlhttprequest");
44        return;
45    }
46
47    var state_callback = function () {
48        if (req.readyState != 4) return;
49
50        if (req.status != 200) {
51            if (opts.onError) opts.onError(req.status ? "status: " + req.status : "no data");
52            return;
53        }
54
55        var resObj;
56        var e;
57        try {
58            eval("resObj = " + req.responseText + ";");
59        } catch (e) {
60        }
61
62        if (e || ! resObj) {
63            if (opts.onError)
64                opts.onError("Error parsing response: \"" + req.responseText + "\"");
65
66            return;
67        }
68
69        if (opts.onData)
70            opts.onData(resObj);
71    };
72
73    req.onreadystatechange = state_callback;
74
75    var method = opts.method || "GET";
76    var data = opts.data || null;
77
78    var url = opts.url;
79    if (opts.method == "GET" && opts.data) {
80        url += url.match(/\?/) ? "&" : "?";
81        url += opts.data
82    }
83
84    url += url.match(/\?/) ? "&" : "?";
85    url += "_rand=" + Math.random();
86
87    req.open(method, url, true);
88
89    // we should send null unless we're in a POST
90    var to_send = null;
91
92    if (method.toUpperCase() == "POST") {
93        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
94        to_send = data;
95    }
96
97    req.send(to_send);
98};
99
100HTTPReq.formEncoded = function (vars) {
101    var enc = [];
102    var e;
103    for (var key in vars) {
104        try {
105            if (!vars.hasOwnProperty(key))
106                continue;
107            enc.push(encodeURIComponent(key) + "=" + encodeURIComponent(vars[key]));
108        } catch( e ) {}
109    }
110    return enc.join("&");
111
112};
113
Note: See TracBrowser for help on using the browser.