04/12: Javascrip > JSONサンプル
Category: Web dev
Posted by: hajime osako
var dummyData = new Object; dummyData[0] = new Object; dummyData[0]['title'] = ""; dummyData[0]['addr1'] = ""; dummyData[0]['addr2'] = ""; dummyData[0]['town'] = ""; dummyData[0]['state'] = ""; dummyData[0]['postcode'] = ""; dummyData[0]['telephone'] = ""; dummyData[0]['fax'] = ""; dummyData[0]['hidden'] = "N"; function fillRetailer(id) { if(id>0) { sendRequest("/heat/web/includes/retailerupload_xmlrpc.php", ("id="+id), callback); } else { tempObj = dummyData[0]; fillForm(tempObj); } } function callback(request) { var tempObj; eval("tempObj = "+request.responseText); if(!tempObj || tempObj=="") { alert("No matching data for the selected retailer."); return false; } fillForm(tempObj); } function fillForm(obj) { for (i in obj) { if(document.getElementById(i)) document.getElementById(i).value = obj[i]; } }
/** * AJAX funcion: Send request to external page * Require createHttpRequest * * @param String url External page * @param String value_str value (eg. a=1&b=2&c=3...) * @param Object funcForSuccess Call back function) * @param Boolean aSync * @param String get_post Method (get or post) * @param Boolean cache Empty or any value. Default true means using cache. * @param Boolean encode Empty or any value. Default false means do not encode * @since 2005/08/30 */ function _sendRequest() { var url = _sendRequest.arguments[0]; var value_str = ""; if(_sendRequest.arguments[1]) value_str = _sendRequest.arguments[1]; var funcForSuccess = false; if(_sendRequest.arguments[2]) funcForSuccess = _sendRequest.arguments[2]; var aSync = true; if(!funcForSuccess) aSync = false; else if(_sendRequest.arguments[3]) aSync = _sendRequest.arguments[3]; var get_post = "GET"; if(_sendRequest.arguments[4]) get_post = _sendRequest.arguments[4]; var cache = true; if(_sendRequest.arguments[5]) cache = _sendRequest.arguments[5]; var encode = false; if(_sendRequest.arguments[6]) encode = _sendRequest.arguments[6]; var tmpResult = ""; var err_msg = ""; var ua = navigator.userAgent; var safari = ua.indexOf("Safari")!=-1; var mozes = ((a=navigator.userAgent.split("Gecko/")[1] )?a.split(" ")[0]:0) >= 20011128; if(url == '') { alert("Error no argument.\n"); return false; } /* AJAX */ var request = _createHttpRequest(); if(safari || mozes) { request.onload = function () { if(funcForSuccess) { /* callback function for success */ funcForSuccess(request); } else { tmpResult = request; } } } else { request.onreadystatechange = function() { if ( request.readyState == 4 ) { //if error status (cf 0 = uninitialized, 1 = loading, 2 = loaded, 3 = interactive, 4 = complete) if (request.status != 200) { /* callback function/procedure for false */ err_msg += "Error retrieving the data:\n" + request.status + " | " + request.statusText + " | " + url +"\n"; } else if(funcForSuccess) { /* callback function for success */ funcForSuccess(request); } else { tmpResult = request; } } } } // if cache is blank, force to get new data if(cache) { if(url.indexOf("?") > 0) url = url + ""; else url = url + "?"; } else { var tmp_date = new Date(); if(url.indexOf("?") > 0) url = url + "&ms=" + tmp_date.getUTCMilliseconds()+"&"; else url = url + "?ms=" + tmp_date.getUTCMilliseconds()+"&"; } // Note: Both POST and GET need to encode var enc_value = ''; if(encode) { var value_ary = value_str.split('&'); for(i=0;i