0

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???

6
  • What php framework are you using? Commented Oct 8, 2014 at 6:59
  • Did you try to setup router (I do not use Zend) for GET and check in browser url /admin/AdminController/testFunction? Do you see any response? Commented Oct 8, 2014 at 7:00
  • @Cheery sir, i get : A 404 error occurred Page not found. Commented Oct 8, 2014 at 7:08
  • But did you add path to the router?? As far as I see (and it is typical to Symfony, that I prefer) function from controllers are assigned in router and their name should end with Action Commented Oct 8, 2014 at 7:09
  • @Cheery means should i rename function name to testFunctionAction??? Commented Oct 8, 2014 at 7:19

2 Answers 2

1

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, i.e. 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'

Sign up to request clarification or add additional context in comments.

6 Comments

I want to return text to ajax from this function
Used echo just now, but no effect
what do you see when opening the url in browser?
nothing. I have a html button from which i am calling above javascript function and wanted to make ajax call to function in php class
@Kuf Failed to load resource: the server responded with a status of 404 (Not Found) example.com/admin/AdminController/testFunction
|
0

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

3 Comments

Failed to load resource: the server responded with a status of 404 (Not Found) example.com/admin/AdminController/testFunction
@ÁngelDiMaría It means that you did not setup the correct route to this function. Methods of the class are not called by url, this is what framework is doing when you are telling it to do so.
@ÁngelDiMaría URL is not a route. In Zend Framework router is a class (or array in the framework's config), that makes correspondence between requested URL and Action Controller and method in it. I'm not sure that you looked at the Zend's manual.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.