473,511 Members | 15,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

javascript not identifying JSON Response as JSON

101 New Member
//Outputs:
Hi ,

I am making a application in which I have to send a request using Ajax to ther server and the server return response in JSON .

I just wanna know i am getting the following response from the server:

Expand|Select|Wrap|Line Numbers
  1. {"length":50,
  2. "accounting":[
  3. {"firstName":"John","lastName":"Doe","age":23},
  4. {"firstName":"Mary","lastName":"Smith","age":32}],
  5. "sales":[
  6. {"firstName":"Sally","lastName":"Green","age":27},
  7. {"firstName":"Jim","lastName":"Galley","age":41}
  8. ]}
In JavaScript how i accesss it. Means when I am tring to access it it is saying undefined

Expand|Select|Wrap|Line Numbers
  1. if (xmlHttp.readyState==4)
  2. {
  3. var JSONResponse = xmlHttp.responseText;
  4. alert (JSONResponse)
  5. document.getElementById("txtHint").innerHTML=JSONResponse.length;
  6. }
  7.  
alert is printing
Expand|Select|Wrap|Line Numbers
  1. {"length":50,
  2. "accounting":[
  3. {"firstName":"John","lastName":"Doe","age":23},
  4. {"firstName":"Mary","lastName":"Smith","age":32}],
  5. "sales":[
  6. {"firstName":"Sally","lastName":"Green","age":27},
  7. {"firstName":"Jim","lastName":"Galley","age":41}
  8. ]}
but document.getElementById("txtHint").innerHTML=JSONR esponse.length; is printing undefined

Please suggest what I am doing wrong in this.


Regards,
Nov 5 '07 #1
5 1741
gits
5,390 Recognized Expert Moderator Expert
hi ...

change the line:

Expand|Select|Wrap|Line Numbers
  1. var JSONResponse = xmlHttp.responseText;
  2.  
to:

Expand|Select|Wrap|Line Numbers
  1. var JSONResponse = eval(xmlHttp.responseText);
kind regards
Nov 5 '07 #2
buntyindia
101 New Member
hi ...

change the line:

Expand|Select|Wrap|Line Numbers
  1. var JSONResponse = xmlHttp.responseText;
  2.  
to:

Expand|Select|Wrap|Line Numbers
  1. var JSONResponse = eval(xmlHttp.responseText);
kind regards
Now it is showing error expected ';' at
Expand|Select|Wrap|Line Numbers
  1.  var JSONResponse = eval(xmlHttp.responseText);
Nov 6 '07 #3
gits
5,390 Recognized Expert Moderator Expert
so you have to show what you have done besides that, because when you test the following:

Expand|Select|Wrap|Line Numbers
  1. var test = eval({"length":50,
  2. "accounting":[
  3. {"firstName":"John","lastName":"Doe","age":23},
  4. {"firstName":"Mary","lastName":"Smith","age":32}],
  5. "sales":[
  6. {"firstName":"Sally","lastName":"Green","age":27},
  7. {"firstName":"Jim","lastName":"Galley","age":41}
  8. ]});
  9.  
  10. alert(test.length);
it alerts 50 as expected.

kind regards
Nov 6 '07 #4
buntyindia
101 New Member
it works Thanks ! :)
Nov 6 '07 #5
gits
5,390 Recognized Expert Moderator Expert
glad to hear that :) ... post back to the forum anytime you have more questions ...

kind regards
Nov 6 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
6742
by: Andy Fish | last post by:
Hi, I have a webpage with some javascript that constructs some data structures, some of which contain nested structures or arrays of other structures. It's the same order of complexity as a...
4
2727
by: andrei.csibi | last post by:
I've have a .NET Assembly, which is COM Visible. I would like to load objects from this assembly in javascript code. E.g. Assembly Book.dll has the class Book and I would like to use <script...
5
2071
by: petermichaux | last post by:
Hi, Some servers return JavaScript as the response to an AJAX request. When the response JavaScript is eval'ed it calls other JavaScript functions already in the browser to update elements, etc....
2
1414
by: Ralph | last post by:
Hi I have problem converting JSON string to an array. The best way to explain this is to show you some code. So : try { var response = that.AjaxReq.responseText.parseJSON(); } catch (myErr)...
10
2651
by: Tom Cole | last post by:
While I've done javascript work for as long as I can remember (since Netscape first released it), I've only ever used it for trivial things, change a color here, validate a text element there, blah...
5
10561
by: Marc | last post by:
I'm aware that there are significant differences between VBScript objects and JScript objects but that doesn't mean something like the following should give me such troubles? <%@...
4
1503
by: Jonathan Fine | last post by:
Hello I want to provide a mini-help system on a web page, to help the user when s/he is filling in a form. I thought I'd hold the help data in JSON format, and translate it to HTML via...
6
2776
by: cantrell78 | last post by:
I can't for the life of me figure out how to execute javascript inside of div that was set using innerHTML or in my case using cloneNode and replaceChild (not my idea to do this, I'm just fixing...
6
3540
Frinavale
by: Frinavale | last post by:
Apparently I have a lot of questions today regarding JavaScript security. I've implemented a JavaScript Object that intercepts page submits (postbacks) and then displays a UI prompting the user...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.