[eluser]Unknown[/eluser]
I use the following the jquery statements to call my php controller function, it gets called but my result is not returned to my success function....
Code: <html>
<head>
<link rel="stylesheet" type="text/css" href="http://localhost/codeigniter_cup_myth/stylesheets/style.css" />
<link rel="stylesheet" type="text/css" href="http://localhost/codeigniter_cup_myth/stylesheets/calendar.css" />
<link rel="stylesheet" type="text/css" href="http://localhost/codeigniter_cup_myth/stylesheets/date_picker.css" />
[removed][removed]
[removed][removed]
[removed]
$(document).ready(function() {
getRecordspage();
});
function getRecordspage() {
$.ajax({
type: "POST",
url:"http://localhost/codeigniter_cup_myth/index.php/adminController/mainAccount",
data: "{}",
contentType: "application/json; charset=utf-8",
global:false,
async: true,
dataType: "json",
success: function(result) {
alert(result);
}
});
}
[removed]
</head>
<body>
<table id="chkbox" cellpadding="0" cellspacing="2" width="100%" class="table_Style_Border">
<tr>
<td class="grid_header" align="center">S.No</td>
<td class="grid_header" align="center">Account Name</td>
<td class="grid_header" align="center">Account Acronym</td>
<td class="grid_header" align="center">Finance Year Start</td>
<td class="grid_header" align="center">Finance Year End</td>
<td class="grid_header" align="center"> </td>
</tr>
<tr> <td colspan="5"> </td></tr>
</table>
</body>
</html>
My controller method,
Code: function mainAccount()
{
$_SESSION['menu'] = 'finance';
$data['account'] = $this->adminmodel->getaccountDetails();
if(empty($data['account']))
{
$data['comment'] = 'No record found !';
}
$json = json_encode($data);
return $json;
}
I get the `alert(1);` in my success function but my `alert(result);` show `null`... Any suggestion... Guys as i am a newbie to this forum my script tag has been stripped from my code i dont know y?
[eluser]mattpointblank[/eluser]
Try echoing your output instead of returning it, in your mainAccount() method. Also, you should probably type alert(result.data) instead.
[eluser]explorercode[/eluser]
Did you ever find the solution for your problem?
I'm familiar with the javascript jquery/ajax, so I would like to use that to integrate with the Restful API. I am learning codeigniter for now. I created an API based on sample code (similar to the one above), but when I use the javascript ajax to consume it, the success data returned is null via JS. I can see the results from the API on the browser, but ajax still says the data variable is null.
I tried echo, return, $this->response($arrayvariable, 200), and all of these give me a null value.
What am I missing? Thanks
[eluser]InsiteFX[/eluser]
Try turning this off.
InsiteFX
[eluser]Cristian Gilè[/eluser]
Code: function getRecordspage() {
$.ajax({
type: "POST",
url:"http://localhost/codeigniter_cup_myth/index.php/adminController/mainAccount",
global:false,
dataType: "json",
success: function(result) {
var obj = eval(result);
alert(obj.account + obj.comment);
}
});
Cristian Gilè
[eluser]explorercode[/eluser]
Thanks for the reply, but the async didn't work. The eval won't work because the 'result' value is null.
I am using the CodeIgniter Restful API example created by Philip Sturgeon in nettusplus - working-with-restful-services-in-codeigniter-2 article
and I am writing my javascript code (below) to read it, but still getting null results - (the variable 'j' in the success method is null)
Any suggestions are appreciated.
Thanks
[eluser]explorercode[/eluser]
Here is my simplified js code
type: "get",
asyc: false,
dataType: 'json',
url:getURL(),
success: function(j){
alert(j);
}
[eluser]BrianJM[/eluser]
The 'data' option must be a key/value pair. Remove the completely. If you want to send non-processed data look at the 'processData' option.
You don't need to 'contentType'. Data sent to the server is automatically transmitted in UTF-8 charset.
Don't eval() the result - not only is it insecure but setting the dataType to 'json' will already evaluate as json data (without using eval()  .
Also, don't set ajax to 'false'. If you want it asynchronous, leave it that way. Synchronous/Asynchronous should not effect your code.
This is what it should look like (below), less the URL - since I am not an active member I cannot submit replies with links so please add the URL back. I also changed your success function for better debugging. Run this and reply with the results.
Code: function getRecordspage() {
$.ajax({
type: "POST",
url: "",
global: false,
async: true,
dataType: "json",
success: function(data, text, xhr){
alert(data);
alert(text);
alert(xhr);
}
});
}
[eluser]explorercode[/eluser]
Thank you for the suggestions, but I am still having a problem.
Here is actually how my controller looks like :
Code: function domain_get(){
$domain = $this->get('id');
$result = $domain;
$this->response($result, 200, 'data'); // 200 being the HTTP response code
}
if I type the URL for this controller, I get the results back, but when I call it from the js below, the data is null, text is success and xhr is the XMLHttpRequest
Code: type: “GET”,
asyc: false,
dataType: ‘json’,
url:getURL(),
success: function(data, text, xhr){
alert(data);
}
Do i have to use POST instead of GET even if I need to read the id from the querystring?
Thanks
[eluser]BrianJM[/eluser]
Try serializing your data in JSON format. It looks like you may have already been doing that?
I'm not sure what the 3rd parameter of the response() function is for?
Code: function domain_get() {
$domain = $this->get('id');
$result['domain'] = $domain;
$this->response(json_encode($result), 200, 'data'); // 200 being the HTTP response code
}
Code: success: function(data, text, xhr){
alert(data.domain);
}
|