Raz*_*van 10 php methods ajax class
嗨,
我想通过ajax从类中调用一个方法.这个类是这样的:
class MyClass{
public function myMethod($someParameter,$someParameter2){
//do something
return $something;
}
private function myMethod2($someParameter3){
//do something
return something;
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用ajax来调用类方法(myMetod(2,3))并返回做某事吗?我能这样用吗?
$.ajax({
url : 'myClass.php',
data : {
someData: '2,3',
}
type : 'POST' ,
success : function(output){
alert(output)
}
});
Run Code Online (Sandbox Code Playgroud)
您需要创建调用此类方法的php脚本,并可以作为ajax请求调用.创建一个文件,如:
例如:
myfile.php
<?php
$date = $_POST; // print_r( $_POST ); to check the data
$obj = new MyClass();
$obj->myMethod( $_POST['field1'], $_POST['field2'] );
$obj->myMethod2( $_POST['field1'] );
?>
Run Code Online (Sandbox Code Playgroud)
并将您的jQuery代码更改为:
$.ajax({
url : 'path/to/myfile.php',
data : { someData: '2,3' },
type : 'POST' ,
success : function( output ) {
alert(output)
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14531 次 |
| 最近记录: |