php - call rest api service from jquery -


i trying understand restful services.i can create class , define function when call through jquery return null , when call directly typing url in address bar return json response

this code

class api extends rest  {         public function processapi()         {             $func = strtolower(trim(str_replace("api/","",$_request['request'])));              if((int)method_exists($this,$func) > 0)             {                     $this->$func();               }             else             {                              $this->response('',404);               // if method not exist in class, response "page not found".             }         }          private function json($data)         {             if(is_array($data))             {                 return json_encode($data);             }         }          public function demo()         {             $error = array('status' => '200', "msg" => "ok");            $this->response($this->json($error), 200);         }  }  $api = new api; $api->processapi(); 

i want call demo method jquery.this trying

$.post("db/api/demo",     function(data,status){       data = jquery.parsejson(data);       alert("data: " + data + "\nstatus: " + status); }); 

i getting response through jquery when demo method this

public function demo() {    $error = array('status' => '200', "msg" => "ok");    echo json_encode($error);    //$this->response(json_encode($error), 200); } 

you should send datatype server.

try code:

$.post( "db/api/demo")       .done(function( data ) {          $.each(data, function(index, element) {             alert('data: ' + element.data + ' status: ' + element.status);            });       }, "json");  

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -