2016-07-18 17 views
3

ajaxを介して別のPHPクラスの特定のメソッドにデータを送信するにはどうすればよいですか? urlの値でクラスファイルを指していますが、使用するメソッド名はどこに割り当てることができますか?Ajaxによるデータ、クラス、メソッドの送信

$.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

data:vidDataにデータを渡し、コントローラの呼び出し後に関数名を指定します。あなたの関数で$_POSTを使用して

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['vidData']であなたのAjaxのデータを取得します。

また、vidDataの代わりにdataを呼び出す必要があります。成功するには、ajax console.log(data)が必要です。

0

リクエストを指示する方法を処理するには、サーバー側のメカニズムが必要です。

のjQueryを::おそらくURLはあなただけのクラス宣言を持っているためにリクエストを送信している...あなたは何をすべきか分かっていないディスパッチャまたは他のPHPのいくつかの並べ替えが必要

$.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'])); 
    } 
} 
関連する問題