Pregunta

I am using $.post to write get a result from a database.

My syntax is:

     $.post('addbundle_summary', {id:id},function(resultsummary) { 
  alert(resultsummary[0]);
     })

I am using codeigniter and in my model I am returning the result as below. note that my sql always returns a single result so $stackid is always a single number:

return $stackid;

my controller send this back to the function with:

$this->output->set_content_type('application/json')->set_output(json_encode($data));

in developer tools, you can see the result from the function as per below image enter image description here

even though the result is 23, my alert is showinf me 2. if the result was 573 it would alert 5.

How can I get this to return the entire result number and not just the first letter of the string?

¿Fue útil?

Solución

alert(resultsummary[0]) is returning resultsummary string at index 0, which is 2. use alert(resultsummary) instead.

$.post('addbundle_summary', {id:id},function(resultsummary) { 
  alert(resultsummary);
})

Otros consejos

Try this: Convert your json data into string before alert

 $.post('addbundle_summary', {id:id},function(resultsummary) { 
  alert(JSON.stringify(resultsummary));
 })
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top