简体   繁体   中英

Call PHP function in particular class via Ajax

I am using Zend Framework.

I have PHP class as:

FileName : AdminController.php Path : /admin/AdminController.php

Ajax Function :

function exportToExcel()
  {

  $.ajax({
  url: "/admin/AdminController/testFunction",
  type: "POST",
  success: function(output){
  alert("Sucess "+output);
  }
  });
 }

PHP Class:

class AdminController extends AbstractActionController
{
  public function testFunction()
  {
    return 'Its a test!!!';
  }
}

But i am not getting alert in sucess as:

Sucess Its a test!!!

What can be mistake?

How to call php function in particular phpclass/file???

try

class AdminController extends AbstractActionController
{
  public function testFunction()
  {
    echo 'Its a test!!!';
  }
}

when you do 'return' the function returns the object, this is how you return data from models to your controllers, but when you need to send data to the client, here using AJAX, you need to print the data.

UPDATE

try to open the url in browser, ie go to http://www.your.domain.com/admin/AdminController/testFunction and if the server is configerd as it should, you should see Its a test!!! on your screen. if you don't see it, follow this guide to configure your server, especially the part about ' Create Route '

Try with full url in ajax call

 url: "http://example.com/admin/AdminController/testFunction",

Make sure you have setup routes for this.

Must be send back a output with echo , print or any printing method

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

For more error check your browser console for errors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM