uu.ajax(), uu.jsonp()

uupaa.js の個々の機能をちょっとずつ紹介しています。

uu.ajax(url, option, callback) は Ajax, uu.jsonp(url, option, callback) は JSONP の機能です。

// uu.ajax - Async request
function uuajax(url,        // @param String: url
                option,     // @param Hash: { xml, data, ifmod, method, timeout,
                            //                header, binary, before, after }
                            //    option.xml     - Boolean(= false): true is xml.responseXML, false is xml.responseText
                            //    option.data    - Mix(= null): upload data
                            //    option.ifmod   - Boolean(= false): true is apply/looup "If-Modified-Since" header
                            //    option.method  - String(= "GET"): "GET", "POST", "PUT"
                            //    option.timeout - Number(= 10): timeout sec
                            //    option.header  - Hash(= {}): { key: "value", ... }
                            //    option.binary  - Boolean(= false): true is binary data
                            //    option.before  - CallbackFunction(= void): before({ option }, xhr)
                            //    option.after   - CallbackFunction(= void): after({ option, ok, rv, status }, xhr)
                callback) { // @param CallbackFunction: callback(response)
                            //    response        - Hash: { ok, rv, date, cached, option, status }
                            //    response.ok     - Boolean(= false): true is status = 20x, false is status = 30x, 40x, 50x
                            //    response.rv     - String(= null): result value. xhr.responseText or xhr.responseXML
                            //    response.date   - DateHash(= null): uu.date(If-Modified-Since) value (option.iftMode)
                            //    response.cached - Boolean(= false): true is Not Modified(xhr.status = 304)
                            //    response.option - Hash(= option): option argument
                            //    response.status - Number(= 400): xhr.status
}
// uu.jsonp - Async JSONP request
function uujsonp(url,        // @param String: "http://example.com/api?callback=@"
                 option,     // @param Hash: { timeout, method }
                             //    option.timeout - Number(= 10): timeout sec
                             //    option.method  - String(= "callback"): callback method name
                             //    option.before  - CallbackFunction(= void): before({ option }, <script>)
                             //    option.after   - CallbackFunction(= void): after({ option, ok, rv, status }, <script>)
                 callback) { // @param CallbackFunction: callback(response)
                             //    response        - Hash: { ok, rv, option, status }
                             //    response.ok     - Boolean(= false): true is status = 20x, false is status = 30x, 40x, 50x
                             //    response.rv     - Mix(= null): result value
                             //    response.option - Hash(= option): option argument
                             //    response.status - Number(= 408): 200 or 408
}

uu.ajax と uu.jsonp の第三引数は、処理完了または失敗で callback(response) の形でコールバックします。

コールバック関数に任意の値を渡す

response には option が含まれるため、uu.ajax() の第二引数(option)に何か値を渡しておくことで、callback 関数内から response.option として参照可能になります。

uu.ajax("http://example.com/api", { timeout: 60, oreore: 123 }, function(resp) {
  if (resp.ok) {
    // resp.option.oreore で参照する
    uu.puff("success oreore=@", resp.option.oreore); // alert("success oreore=123");
    uu.puff(resp.rv.length); // alert(responseText.length);
  } else {
    uu.puff("fail");
  }
});

uu.jsonp("http://example.com/jsonp?callback=@", { timeout: 60, oreore: 123 }, function(resp) {
  if (resp.ok) {
    // resp.option.oreore で参照する
    uu.puff("success oreore=@", resp.option.oreore); // alert("success oreore=123");
  } else {
    uu.puff("fail");
  }
});