2016-07-18 7 views
3

Wie kann ich Daten über Ajax an eine bestimmte Methode in einer anderen PHP-Klasse senden? Im url Wert habe ich auf die Klassendatei hingewiesen, aber wo kann ich den Methodennamen zuweisen?Senden von Daten, Klassen und Methoden über 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"); 
     } 
    }); 

Antwort

0

Führen Sie die Daten in data:vidData und Ihre Funktionsnamen nach Aufruf des Controller angeben.

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"); 
     } 
    }); 

Mit $_POST in Ihrer Funktion werden Sie Ihre Ajax-Daten in $_POST['vidData'] erhalten.

Auch müssen Sie data statt vidData Variable in Erfolg von AJAX console.log(data) aufrufen.

0

Sie benötigen einen serverseitigen Mechanismus, um die Anforderungen Ihrer Anforderungen zu steuern. Vermutlich ist die URL, die Sie eine Anfrage senden nur die Klassendeklaration hat ... müssen Sie irgendeine Art von Dispatcher oder auch PHP nicht weiß, was zu tun ist:

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'])); 
    } 
} 
Verwandte Themen