jquery.ajax({ type: "post", url: 'your_functions_address.php', datatype: 'json', data: {functionname: 'add', arguments: [1, 2]}, success: function (obj, textstatus) { if( !('error' in obj) ) { yourvariable = obj.result; } else { console.log(obj.error); } }});
您的_function_address.php如下所示:
<?php header('content-type: application/json'); $aresult = array(); if( !isset($_post['functionname']) ) { $aresult['error'] = 'no function name!'; } if( !isset($_post['arguments']) ) { $aresult['error'] = 'no function arguments!'; } if( !isset($aresult['error']) ) { switch($_post['functionname']) { case 'add': if( !is_array($_post['arguments']) || (count($_post['arguments']) < 2) ) { $aresult['error'] = 'error in arguments!'; } else { $aresult['result'] = add(floatval($_post['arguments'][0]), floatval($_post['arguments'][1])); } break; default: $aresult['error'] = 'not found function '.$_post['functionname'].'!'; break; } } echo json_encode($aresult);?>
推荐:php服务器
以上就是js如何调用php函数的详细内容。
