Reputation: 3779
I've come across a situation where I would appreciate some help with.
I have a controller called Site with a function as follows:
function getID(){
$id_data = $this->uri->segment(3);
$name = $this -> session -> userdata('name');
echo json_encode($this -> getID_model -> getID($id_data,$name ));
}
my url looks as follows
http://myapp.com/index.php?/site/user/96
when the link is opened the user is able to see his page only when the User function has ran my checks. This is why I can not remove or alter the User function since it needs to read the URI data and needs to be kept separate from my getID function
Now here is where my question comes in.
I need to make a call to the getID function in the Site controller and it needs the 3rd URI id number.
Below is my JQuery
var fullurl = $('#hiddenurl').val() + 'index.php?/site/getID/';
$.getJSON(fullurl, function(json) {
$.each(json, function(i,d) {
//do my output stuff.
});
$('#_description').append(output);
});
This won't work because the controller expects a 3rd URI segment.
Can someone tell me how I should add this segment in to the url? should I use some sort of JS parsing?
Thank you.
Upvotes: 1
Views: 2378
Reputation: 1284
if you wanto to get json fist you need to send json not plain text
function getID(){
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
$id_data = $this->uri->segment(3);
$name = $this -> session -> userdata('name');
echo json_encode($this -> getID_model -> getID($id_data,$name ));
}
and this for js
var valueofsometing=$('#someitng').val();
var base_url = <?=base_url()?>
jQuery.ajax({
url:base_url + "/site/getID/" + valueofsometing,
async:false,
type:'GET',
cache:false,
timeout:30000,
success:function (result) {
$('#joblist').append(result);
$("select.workflow_class").last().uniform()
initdrag();
}
});
Upvotes: 0
Reputation: 690
Surely you have access to (or can get access to) the $id_data
variable in the view where you render your "#hiddenurl" element? You could do something like:
<input type="hidden" name="whatever" id="whatever" value="<?php echo site_url("site/getID/$id_data"); ?>">
Then you will have easy access to the entire URL that you want to use in your getJSON() call.
var my_url = $('#whatever').val();
// Now my_url should contain something like http://example.com/index.php/site/getID/123
Note: you can only use site_url()
rather than the longer $this->config->site_url()
to generate URLs if you have loaded CodeIgniter's url helper...
Upvotes: 2