Cause Analysis of json data obtained by ajax as undefined, jsonundefined

Source: Internet
Author: User

Cause Analysis of json data obtained by ajax as undefined, jsonundefined

Asynchronous JavaScript and XML (Ajax) is a key technology driving the next generation of Web sites (commonly referred to as Web 2.0 sites. Ajax allows data retrieval in the background without interfering with the display and behavior of Web applications. The XMLHttpRequest function is used to obtain data. It is an API that allows the client JavaScript to connect to a remote server over HTTP. Ajax is also the driving force of many MashUps, which can integrate content from multiple places into a single Web application.

When processing the json value sent from the server, we can process it in two ways:

1. Use the eval () function.var dataObj=eval("("+data+")");// Convert to a json object (Note: data is json data );

2. Use the Function object for return parsing.var json =(new Function("","return "+data))();

However, when I am working on a project today (the project uses struts2), I use the two methods for json reading, and the output is always undefined. I checked a lot of information online, and most of them did not understand it. Later I found an article and the author encountered this problem. His solution was to declare the json attribute in the return value in the action, the test fails on your project, and the solution is to switch it again. The following are your own ideas: although the data transmitted from the background is declared as a json attribute, it is actually a string when it is uploaded to the foreground, not a json object, at this time, we need to process the background data, convert it to a json object, and then obtain the value in json. However, sometimes the value sent from the background is not necessarily a string in the standard json format. Although we use one of the preceding two methods to convert the object transmitted from the background, however, the converted json object is actually a string, which is a standard json string. After another conversion, it becomes a json object.

Some code is as follows:

Result = JSONObject. fromObject (map ). toString (); // background code, convert map to json object success: function (result) {// code returned from the foreground var obj = eval ("(" + result + ") "); // var data1 = (new Function (" "," return "+ obj) (); var data1 = eval (" + obj + ") "); alert (data1.name );}

Summary

The above is an analysis of the reason why ajax obtains json data as undefined. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.