javascript $.post returning correct result but cant catch all characters in returned string

StackOverflow https://stackoverflow.com//questions/23059708

Вопрос

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?

Это было полезно?

Решение

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

Другие советы

Try this: Convert your json data into string before alert

 $.post('addbundle_summary', {id:id},function(resultsummary) { 
  alert(JSON.stringify(resultsummary));
 })
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top