好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

js如何调用php函数

js调用php函数的方法:

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函数的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于js如何调用php函数的详细内容...

  阅读:66次