473,668 Members | 2,519 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.getEle mentById("txtHi nt").innerHTML= JSONResponse.le ngth; is printing undefined

Please suggest what I am doing wrong in this.


Regards,
Nov 5 '07 #1
5 1750
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
6749
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 web.xml file, say. I need to post the whole lot to the server and I'm not sure which is the best way to do it. It has to be browser-independent. The only ways I can think of are:
4
2744
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 language="javascript" type="text/javascript"> var book = new Book(); document.write(book.Title); </script> Even more, if I could access book through the document object it would be even more helpful, like: <script language="javascript"...
5
2079
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. This seems like a good system because it allows so much freedom in creating the desired behavior in the browser. The required data doesn't have to be converted to XML or JSON on the server. The browser doesn't have to have templates for...
2
1419
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) { alert('The fatal error occured. Please reload the page.'); return;
10
2664
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 blah blah. With Ajax (actually, the uncovering of the XmlHTTPRequest object) I absolutely see the benefit of moving more of the UI work to the client, rather than doing page refreshes. I know there are a bunch of libraries out there...
5
10571
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? <%@ Language=VBScript %> <script language="jscript" runat="server"> var json = {"widget":}; </script> <% Response.Write json.widget & "<br />"
4
1506
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 JavaScript in response to user actions. So I'm wanting to translate suitable JSON into HTML elements. I've looked around a bit and not found anything that does this.
6
2787
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 it). I have tried it with and without the defer attribute. It will work fine in firefox, but not IE. <script type="text/javascript"> function setInnerHTMLAndExecScript (element, html) { var newElement = element.cloneNode(false);...
6
3546
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 to confirm(yes)/deny(no)/cancel(close UI/cancel submit) their action. There may be additional JavaScript methods to execute before displaying the UI and may be additional JavaScript methods to execute upon closing the UI. I'm thinking about...
0
8367
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8889
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8570
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8650
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6206
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.