I have a php function that returns json data that looks like this:
if ($result !== false)
{
$resultdata = array(
'status' => true,
'error_msg'=>'',
'data' => $result
);
}
else
{
$resultdata = array(
'status' => false,
'error_msg'=>"Unable to reassign port vlan. Check Port mode.",
'data' => $result
);
}
header ('Content-Type: application/json; charset=UTF-8');
echo json_encode($resultdata);
The data from the model ($result) is built like this:
$pattern = "/([0-9]+)(\s+)([0-9]+)(\s+)([a-z]+)(\s+)([a-z]+)(\s+)/i";
if (preg_match($pattern, $this->_data, $matches))
{
$vlandetailsArray['VlanId']= $matches[1];
$vlandetailsArray['Name']= $matches[3];
$vlandetailsArray['Egress']= $matches[5];
$vlandetailsArray['Type']= $matches[7];
}
And the code in my view looks like this:
$.ajax({
url:fullpath,
type:'POST',
dataType:'json',
success: function(returnDataFromController) {
console.log(returnDataFromController);
if (returnDataFromController.status == true)
{
//build table contents
var htmlstring = '<thead>';
htmlstring = htmlstring + '<tr>';
htmlstring = htmlstring + '<th>VlanId</th>';
htmlstring = htmlstring + '<th>Name</th>';
htmlstring = htmlstring + '<th>Egress Rule</th>';
htmlstring = htmlstring + '<th>Port Membership Type</th>';
htmlstring = htmlstring + '<tbody>';
//should only be one entry so no need to loop
// htmlstring = htmlstring + "<tr>"
//htmlstring = htmlstring + "<td class ='vlanid'>" + returnDataFromController.VlanId + "</td>";
//htmlstring = htmlstring + "<td>" + returnDataFromController.Name + "</td>";
//htmlstring = htmlstring + "<td>" + returnDataFromController.Egress + "</td>";
// htmlstring = htmlstring + "<td>" + returnDataFromController.Type +"</td>";
//htmlstring = htmlstring + "</tr>";
$('#assignedvlans').html(htmlstring);
$('#progress-indicator').hide();
}
else
{
alert('my error message: ' + returnDataFromController.error_msg);
}
},//end success
error: function(jqXHR, textStatus, errorThrown)
{
alert(errorThrown);
}
What I'm wondering is how to access the "data" section of my return array, and the "error_msg". Right now, after the ajax is executed, when the status is false, the alert that appears shows "my error message:undefined". But I'm not getting any error messages about "returnDataFromController.status"
As far as the data is concerned, I've tried :
returnDataFromController.data.VlanId
and
returnDataFromController["#data"].VlanId
but neither one seems to work. The code that's been commented out used to work when i wasn't returning a wrapper array around the data . i was previously just returning "$result" from the controller...
Any suggestions would be appreciated. Thanks.
UPDATE 1 By the way, the console.log doesn't really help me much. For some reason, in the web console, i don't see any data being dumped there. I'm using firefox...
UPDATE 2
When the controller returns true for the status, this is the error message I get from the ajax error handler:
SyntaxError: JSON.parse: unexpected character
I have changed my code to use:
returnDataFromController.data.VlanId
UPDATE 3
To simplify matters, I tried to recreate my problems using more basic code. To that end, here's my controller:
public function test()
{
$mydata = array(
'VlanID'=>80,
'Name'=>'test vlan'
);
$data = array(
'status'=> true,
'error_msg'=>'all is well',
'data'=>$mydata
);
header ('Content-Type: application/json; charset=UTF-8');
echo json_encode($data);
}
And here's my view:
$('#testbutton').click(function() {
$.ajax({
url:'http://myserver/myapp/index.php/controller/test/',
type:'POST',
dataType:'json',
success: function(returnDataFromController) {
alert('success');
console.log(returnDataFromController);
alert(returnDataFromController.data.VlanID);
},//end success
error: function(jqXHR, textStatus, errorThrown)
{
alert('ajax error handler: ' + errorThrown);
}
}); //end ajax call
When i click on the test button, this is what happens: 1. i get the first alert that says "success". 2. nothing is written to the console 3. i get the following error message in the console - when trying to do the second alert "TypeError: returnDataFromController.data is undefined".
hope this example simplifies things and helps to id where I'm going wrong. thanks.
UPDATE 4
When I add:
alert(returnDataFromController.toSource());
I get the following output:
({VlanID:80, Name:"test vlan"})