0

I'm trying to access my rest api which I have created. Its working perfectly fine, when I type the following:

http://localhost:8080/rest/index.php/api/practice/test/name/Peter/surname/Potter/format/json

I get a correct json response. Now I have a website and simply just want to access the rest api using ajax. Here is the code:

$(document).on('pagebeforeshow','#page2',
    function(){
                $('#submit').click(function() 
                {
                    var name = $("#username").val();
                    var surname = $("#usersurname").val();

                    alert(name + " " + surname);
                    //alert("http://localhost:8080/rest/index.php/api/practice/test/name/"+name+"/surname/"+surname);

                    $.getJSON({ 
                           type: "GET",
                           crossDomain: true,
                           dataType: "jsonp",
                           url: "http://localhost:8080/rest/index.php/api/practice/test/name/"+name+"/surname/"+surname,
                           success: function(data)
                           {        
                             alert("workings");
                           }
});
                });         
              });

Now when I use this code I get a 404 not found response. I know for a fact when I got to that url that I'm meant to get a json response. Here is my Controller from the rest api:

<?php  
require(APPPATH.'libraries/REST_Controller.php');  
class practice extends REST_Controller 
{  
    function test_get()
    {
        //echo "working fine ";
        $name = $this->get('name');
        $surname = $this->get('surname');
        //echo $name." ".$surname;
        $result = array('result' => "Working likes a boss ".$name." ".$surname);
        $this->response($result,200);
    }
}
?> 
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
user481610
  • 3,230
  • 4
  • 54
  • 101
  • I'm assuming that when it `works`, you are trying it on the same computer that has the CodeIgniter application running? Localhost is locally translated to 127.0.0.1, which means the device your calling this javascript on, will be attempting to access your CodeIgniter application on 127.0.0.1:8080. You will need to use the local ip address and ensure that the server (running the CodeIgniter application) has opened the Firewall for port 8080. – Gavin Jan 30 '13 at 10:46
  • By work I mean when I go into Google Chrome and type in the above http link I can access my rest api and it returns me everything. Also I'm developing locally on my pc but the ajax code and the rest api and separate from each other. So in theory I would have the ajax on one server call the rest api stored on another server. I've read up on Access-Control-Allow-Origin. Hence why I've changed my code to jsonp. I hope this helps explain things better – user481610 Jan 30 '13 at 10:58

2 Answers2

1

In your call $.getJSON(...), you have the URL

url: "http://localhost:8080/rest/index.php/api/practice/test/name/"+name+"/surname/"+surname

which misses the /format/json part from above.

You also have

dataType: "jsonp",

which isn't json.

Update:

I just looked up jQuery.getJSON() and the call is

jQuery.getJSON( url [, data ] [, success(data, textStatus, jqXHR) ] )

It seems you must change your call to

$.getJSON("http://localhost:8080/rest/index.php/api/practice/test/name/"+name+"/surname/"+surname + "/format/json",
          function(data) { alert("workings"); });

or use jQuery.ajax()

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • I've done the url changes in the ajax and still have the same problem. I also changed my http url format form json to jsonp and my rest service responds with my json inside brackets – user481610 Jan 30 '13 at 11:03
0

you should set header to json :

<?php  
require(APPPATH.'libraries/REST_Controller.php');  
class practice extends REST_Controller 
{  
    function test_get()
    {
        //echo "working fine ";
        $name = $this->get('name');
        $surname = $this->get('surname');
        //echo $name." ".$surname;
        $result = array('result' => "Working likes a boss ".$name." ".$surname);
        header('Content-Type: application/json');
        echo json_encode( $result );
    }
}
?>