, , ajax , , , . , , , . ajax-, script. , :
var rqPHP = {
url:'php/dispatcher.php', type:'POST', dataType:'json',
success:function(json, status, jXHR){
if(!json) return console.warn('[rqPHP.success] json is null');
if(!json.cmd) return console.warn('[rqPHP.success] json.cmd is null');
if(!json.res) return console.warn('[rqPHP.success] json.res is null');
if(json.err && json.err.length){ console.warn('[rqPHP.success errors cmd:'+json.cmd+'] '+json.err);}
switch(json.cmd){
case 'loadfile' :
break;
case 'savefile' :
break;
}
},
error:function(jXHR, status, err){
console.warn('[rqPHP.error] ', status,',',err,',',jXHR.responseText);
}
};
, . json-, , , , , :
rqPHP.data = {'cmd':'loadfile', 'filename':'file.dat', 'arg2':'other argument'};
$.ajax(rqPHP);
script, :
$pv = $_POST;
$res = '';
$err = array();
switch(strtolower($pv['cmd'])){
case 'savefile' :
break;
case 'loadfile' :
if(any error){
$err[] = $loadError;
}else{
$res = ',"res":"'.$dataLoaded.'"';
}
break;
}
$jsonRes = '{"cmd":"'.$pv['cmd'].'"'.$res.',"err":"'.implode('|', $err).'"}';
print $jsonRes;
, , , , :
- requestObject.data URL-, , , jQuery
- POST, URL , post vars "".
- , script, 'json' dataType, , . ajax, , URL- , , script, , script , , , "loadfile", fileUrl , , ...
( , ajax ), ,
function rqSuccess(json, status, jXHR){
console.log('myTarget is : ', this.myTarget, ' , myVariable is : ', this.myVariable);
}
function rqError(jXHR, status, err){
}
var myRq = {url:'dispatcher.php',type:'POST',dataType:'json',
success:rqSuccess,
error:rqError,
myTarget:$('#myblock'),
myVariable:'Hello !',
data : {'cmd':'loadfile',...}
}
$.ajax(myRq);
var myRq2 = {...myTarget:$('#anotherblock'), data:{'cmd':'anotheraction'}...}
$.ajax(myRq2);