Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过Ajax调用特定类中的PHP函数_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 通过Ajax调用特定类中的PHP函数

Javascript 通过Ajax调用特定类中的PHP函数,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我正在使用Zend框架 我的PHP类是: 文件名:AdminController.php 路径:/admin/AdminController.php Ajax函数: function exportToExcel() { $.ajax({ url: "/admin/AdminController/testFunction", type: "POST", success: function(output){ alert("Sucess "+output); } })

我正在使用Zend框架

我的PHP类是:

文件名:AdminController.php 路径:/admin/AdminController.php

Ajax函数:

function exportToExcel()
  {

  $.ajax({
  url: "/admin/AdminController/testFunction",
  type: "POST",
  success: function(output){
  alert("Sucess "+output);
  }
  });
 }
class AdminController extends AbstractActionController
{
  public function testFunction()
  {
    return 'Its a test!!!';
  }
}
PHP类:

function exportToExcel()
  {

  $.ajax({
  url: "/admin/AdminController/testFunction",
  type: "POST",
  success: function(output){
  alert("Sucess "+output);
  }
  });
 }
class AdminController extends AbstractActionController
{
  public function testFunction()
  {
    return 'Its a test!!!';
  }
}
但我在成功中并不警觉,因为:

Sucess Its a test!!!
什么是错误

如何在特定的phpclass/file中调用php函数???

试试看

class AdminController extends AbstractActionController
{
  public function testFunction()
  {
    echo 'Its a test!!!';
  }
}
当您执行“返回”操作时,函数将返回对象,这就是您将数据从模型返回到控制器的方式,但是当您需要使用AJAX将数据发送到客户端时,您需要打印数据

更新

尝试在浏览器中打开url,即转到,如果服务器配置正确,您应该看到
这是一个测试在屏幕上显示。

如果看不到,请按照下面的说明配置服务器,特别是关于“”的部分

 url: "http://example.com/admin/AdminController/testFunction",
确保您已为此设置了路由

必须使用回波、打印或任何打印方法返回输出

public function testFunction() {
  echo 'Its a test!!!';
}

有关更多错误,请检查您的浏览器控制台是否有错误

您使用的是什么php框架?您是否尝试为获取和签入浏览器url设置路由器(我不使用Zend)
/admin/AdminController/testFunction
?你看到什么回应了吗?@Cheery先生,我得到:一个404错误页面没有找到。但是你添加了路由器的路径吗??据我所见(我更喜欢Symfony的典型情况),来自控制器的功能在路由器中分配,它们的名称应以Action@Cheery意味着我应该将函数名重命名为testFunctionAction吗???我想从刚才使用的这个函数返回文本到ajax,但没有效果在浏览器中打开url时会看到什么?什么都没有。我有一个html按钮,在上面调用javascript函数,并希望在php中对函数进行ajax调用class@Kuf未能加载资源:服务器以404(未找到)状态响应example.com/admin/AdminController/testfunction未能加载资源:服务器以404(未找到)状态响应@engeldimaría这意味着您没有设置到该功能的正确路径。类的方法不由url调用,这是框架在告诉它这样做时所做的。@engeldimaría url不是路由。在Zend Framework中,router是一个类(或框架配置中的数组),用于在请求的URL和其中的操作控制器和方法之间进行通信。我不确定你是否看过Zend的手册。