• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList in javascript

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using Ajax to fetch the arraylist from server. My Ajax code is working fine to fetch the data from server, following is the result i am getting when I alert (xmlhttp.responseText) = [1,2,3]

When I am trying to iterate using below code it return wrong value:

Var returnval = [1,2,3]
var newval = returnval.split(",");

for (var i in newval)
{
alert(i)
}

it return me following:
[1
2
3]

not sure why i am getting "[" and "]" in first and last alert. Can some one please help me to resolve this.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please take the time to choose an appropriate forum for your posts. This forum is for questions on JSP. For more information, please click this link ⇒ CarefullyChooseOneForum.

This post has been moved to a more appropriate forum.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's doing exactly what you told it to do. You have a string that contains [1,2,3] and split it on the commas. That results in [1 2 and 3].

Why should this surprise you? It's just text.

If you intend for the return value to be interpreted as a list of values, you need to interpret it as JSON.
 
accnit Jai
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please give me one example? I am completely stuck here, I am new to Javascript. I mean , I do not want "[" and "]".
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you want the results as text or numeric? Giving important details like this helps you get answers faster.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Info on using JSON in JavaScript.
 
accnit Jai
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply, I want result in numeric.
 
accnit Jai
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when I am doing following it is returning Syntax error in Internet Explorer .
eval('(' + xmlhttp.responseText+ ')')

 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With the way you code is written above [after fixing the capital V error] would return the following error
TypeError: returnval.split is not a function

What is xmlhttp.responseText?

Eric
 
accnit Jai
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
xmlhttp.ResponseText contain the ArrayList return from server. It has the value [1,2,3]. I want this value should be printed in 1 2 3 in my java script code, but first value shows "[1 " and last value "3]". please let me know how i can remove "[" and "]" with java script only.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

accnit Jai wrote:xmlhttp.ResponseText contain the ArrayList return from server.

It does not. It contains text.

You were on the right track trying to convert the string from JSON to an array. Why did you give up?

P.S. Make it easy on yourself and use jQuery's $.getJSON() function.
 
accnit Jai
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your reply, we can not use JQuery in our project.
when I am doing following it is returning Syntax error in Internet Explorer .
eval('(' + xmlhttp.responseText+ ')')

Can you please help me to resolve above Syntax error
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not as long as you keep the details a secret.
 
knowledge is the difference between drudgery and strategic action -- tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic