您好,欢迎来到保捱科技网。
搜索
您的当前位置:首页js如何调用php函数

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服务器

Copyright © 2019- baoaiwan.cn 版权所有 赣ICP备2024042794号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务