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);
}
}
?>
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.