Anoniem Anoniem
Anoniem Anoniem

Reputation: 2005

PHP RestServer Class - Ajax call

I'm practicing with the PHP RestServer Class. But I can't get the correct data from it if I use an Ajax call on it. I have the following code:

<?php

require_once "locationOfRestServer.php";

class HelloWorld
{
    public static function sayHello()
    {
        return array("Response" => "Hello World");
    }
}

$rest = new RestServer('HelloWorld');
$rest->handle();

And in my javascript file I use the following:

this.helloWorld = function() {
    $.ajax({
        url: 'locationOfHelloWorld.php'
        type: 'POST',
        dataType: 'json',
        success: function(data){
            console.log(data);
        }
    });
};

I get the following :

error: "No method was requested."

Because; whenever I use it, I have to go to localhost/HelloWorld.php?method=sayHello which is actually working. So I added the following line to the ajax call:

method: 'sayHello',

But it still keeps giving me the same error.

Upvotes: 0

Views: 113

Answers (1)

Rohan Kumar
Rohan Kumar

Reputation: 40639

Try this,

$.ajax({
    url: 'locationOfHelloWorld.php'
    type: 'GET',// use GET method according to your working url
    data:{method: 'sayHello'},// use method in data parameter
    dataType: 'json',
    success: function(data){
        console.log(data);
    }
});

Upvotes: 1

Related Questions