Welcome Guest, Not a member yet? Register   Sign In
[philsturgeon’s REST Api] Always returning 0 => Unknown Method
#1

[eluser]DevTyme[/eluser]
Hi there, I have a jquery ajax calling a get method to my api, but no matter what I always get a response saying 0, unknown method.

I looked around the api and it seems that the api calls
Code:
$this->input->server('REQUEST_METHOD')
to determine if its a get, delete, post, or put method.

Do i need to create any custom headers or anything? No matter what it seems that it can't recognize the type of call to the REST server.

My test function looks as follows:

Code:
require APPPATH.'/libraries/REST_Controller.php';

class api extends REST_Controller {    
    
    function mytest($x){
        $a = $this->get('test');
        $test = array('foo'=>'bar');
        $this->response($test,200);
    }
}

I make my call like so:
Code:
$.ajax({
    url: "http://mydomain.com/api/mytest",
    data: "format=jsonp&test=helloworld",
    dataType: "jsonp",
    type: "GET",
    success: function(data) {
        //alert('success');
    },
    error: function(err, msg){
        //alert(msg);
    }
});
#2

[eluser]Phil Sturgeon[/eluser]
You are calling the REST Server via GET, so the function must be:

Code:
function mytest_get()
#3

[eluser]DevTyme[/eluser]
Dear phil,
i love you and your hasty responses.
never had such a helpful author.

it worked, you're the man.
#4

[eluser]crunk2[/eluser]
Phil,
I have loved using your REST api in previous jobs. However, I am running into an issue. I'm using CI 2.0.0. I have a controller under /api/products and I want to do a GET on it. So, I implemented the index_get() function, but if I try a request like /api/products/id/1, it always comes up with method unknown. Did I misconfigure something?
Scott
#5

[eluser]Keymaker[/eluser]
Salam,

Try make GET request like this:
http://page.com/index.php/here_your_cont...here_value
#6

[eluser]Keymaker[/eluser]
and check or your method get var like: $_GET['here_get_var'];
#7

[eluser]crunk2[/eluser]
Whenever I append something at the end of the URL like /api/products?id=1, I get:

Code:
This page contains the following errors:

error on line 10 at column 7: Extra content at the end of the document
Below is a rendering of the page up to the first error.

A PHP Error was encountered Severity: Warning Message: html_entity_decode() expects parameter 1 to be string, resource given Filename: libraries/Format.php Line Number: 135



I am not sure if I have CI configured correctly to allow GET variables though. I have tried enabling/disabling $config['enable_query_strings'] to see if I can get it working, but to no avail.
#8

[eluser]Keymaker[/eluser]
i am not sure, but try load this libraries before any request:

public function index()
{
$this->support_files();
$this->page_id_handler();
}

function support_files(){
$this->output->set_header("HTTP/1.1 200 OK");
$this->load->library('javascript');
$this->load->library('javascript/jquery');
$this->load->library('session');
$this->load->helper('array');
$this->load->helper('date');
$this->load->helper('url');
$this->load->database();
}

In another words, insert $this->support_files(); where function start. Like

function products() {
$this->support_files();
$id=$this->input->get_post('id', TRUE); // TRUE attribute for cross scripting filter
if ($id){
//here you code and etc.
}

}

there are a lot ways to do it!!! just test it! Maby need some library for ajax???
#9

[eluser]crunk2[/eluser]
It seems like the function is never even called. If I make a call to a normal CI_Controller with query variables I get no problem. It seems to only be with the REST_Controller
#10

[eluser]Anuja[/eluser]
Hello,
I am also fetching the same error.

Code is working fine on browser but from device side it will gives the unknown method error

please help.




Theme © iAndrew 2016 - Forum software by © MyBB