Ajax Responce

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ChrisBull
Forum Commoner
Posts: 42
Joined: Fri Aug 20, 2010 7:43 am

Ajax Responce

Post by ChrisBull »

Hi,Im using jquery to call a php file and then display the responce via alert. (in cakephp).
Here's my controller;

Code: Select all

public function login()
    {
        echo "hello";
    }
And my Jquery Code;

Code: Select all

function sendToPHP() 
{
    $.ajax({
        url:"http://www.testserver.com/accounts/login",
        type:"POST", 
        data:$('#loginForm').serialize(), 
        success:responseLogin, 
        context:this
    });
}
function responseLogin(response)
{
    alert(response);
}
So it should displa 'Hello'in the alert box but is is instead displaying the view for the 'login' action, which is the page we are already on.

Any Ideas?
Many Thanks
Chris
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Ajax Responce

Post by maxx99 »

First, there is no need to url:"http://www.testserver.com/accounts/login" you won't be able to (and you shouldn't) do Cross-Site request (due to security reasons). So /accounts/login should be enough :)

Im not familiar with CakePHP but are you sure you're calling right url/action?
http://www.testserver.com/accounts/login should display only: "hello" is it the case?

Again im not familiar with CakePHP but if login is one of your actions there is a possibility that rest of the layout is sent with the response.
In Zend Framework e.g. you need to call

Code: Select all

  Zend_Layout::resetMvcInstance();
  $this->getResponse()->clearBody();
to display only hello for

Code: Select all

public function loginAction(){
echo "hello";
}
Post Reply