如何通过ajax将数据发送到另一个PHP类中的特定方法?在url
值中,我指向了类文件,但在哪里可以指定要使用的方法名?
$.ajax({
type:'POST',
url:'ResortController.php',
data: vidData,
cache:false,
contentType: false,
processData: false,
success:function(data){
console.log("success");
console.log(vidData);
//window.location.reload();
},
error: function(data){
console.log("error");
}
});
答案 0 :(得分:0)
传递data:vidData
中的数据并在调用控制器后指定您的函数名称。
url = BASE_PATH + 'ResortController/FUNCTION_NAME';
vidData = {id: 123, vidName: "testVideo"};
$.ajax({
type:'POST',
url:url,
data: vidData,
cache:false,
contentType: false,
processData: false,
success:function(data){
console.log("success");
console.log(data);
//window.location.reload();
},
error: function(data){
console.log("error");
}
});
在您的函数中使用$_POST
,您将获得$_POST['vidData']
中的ajax数据。
此外,您需要在ajax data
成功时拨打vidData
而不是console.log(data)
变量。
答案 1 :(得分:0)
您需要有一个服务器端机制来处理如何定向您的请求。大概是你发送请求的网址只有类声明......你需要某种调度程序,否则php不知道该怎么做:
<强> jQuery的:强>
$.ajax({
type:'POST',
url:'/dispatcher.php',
data: {
"data":vidData,
"class":"ResortController",
"method":"rMethod"
},
cache:false,
success:function(data){
console.log("success");
console.log(vidData);
//window.location.reload();
},
error: function(data){
console.log("error");
}
});
<强> /dispatcher.php 强>
<?php
// This is dangerous if you have no controls in place to allow/restrict
// a program to run a command
// Anyone could send a cURL request and run an automated class/method through
// this mechanism unless you have some way to restrict that
if(!empty($_POST['class']) && !empty($_POST['method'])) {
// Here you want to have some way to check that a request is valid and not
// made from some outside source to run arbitrary commands
// I will pretend you have an admin identifier function....
if(is_admin()) {
call_user_func_array(array($_POST['class'],$_POST['method']),array('data'=>$_POST['data']));
}
}