Home > Web Front-end > JS Tutorial > body text

Analysis of the reasons why ajax obtains json data as undefined_AJAX related

韦小宝
Release: 2017-12-16 14:24:49
Original
2111 people have browsed it

Ajax allows data retrieval in the background without interfering with the display and behavior of the web application. This article mainly introduces the reason why ajax obtains json data as undefined. Friends who are interested in ajax can refer to

Asynchronous JavaScript and XML (Ajax) is the driver of the new generation of Web sites (the popular term is Web 2.0 site) key technologies. Ajax allows data retrieval in the background without interfering with the display and behavior of the Web application. Get data using the XMLHttpRequest function, an API that allows client-side JavaScript to connect to a remote server over HTTP. Ajax is also the driving force behind many mashups, which integrate content from multiple places into a single web application.

Generally when processing the json value sent from the server, we will process it in two ways:

1. One is to use the eval() function. var dataObj=eval("("+data+")");//Convert to json object (note: data is json data);

2. Use Function object to return parse. var json =(new Function("","return "+data))();

But when I was working on a project today (the project used struts2), I used these two methods to read json Take, the output is always undefined. I checked a lot of information on the Internet, but most of them couldn't be explained. Later, I found an article where the author also encountered this situation. His solution was to declare the return value as a json attribute in the action. After testing it on his own project, it failed. , then the solution was to transfer it again. The following is my own thinking: Although the data transmitted from the background is declared as a json attribute, when it is passed to the front desk, it is actually a string, not a json object. At this time, we need to process the data in the background and convert it into a json object. Then get the value in json. But sometimes the value transmitted from the background is not necessarily a standard json format string. Although we convert the object transmitted from the background through one of the previous two methods, the json object converted at this time is actually still a string. It is a standard json string, and after we convert it again, it becomes a json object.

The following is part of the code:

 result = JSONObject.fromObject(map).toString();//后台代码,将map转成json对象
      success : function(result) {//前台返回值代码
var obj = eval("("+result+")");
//var data1 =(new Function("","return "+obj))();
var data1=eval("("+obj+")");
alert(data1.name);
      }
Copy after login

##Summary

Above The above is the analysis of the reasons why ajax obtains json data as undefined introduced by the editor. I hope it will be helpful to everyone! !

Related recommendations:

How to implement asynchronous synchronous requests in AJAX

How to implement AJAX and JSONP with native JS

Ajax’s multiple methods of asynchronous file upload

The above is the detailed content of Analysis of the reasons why ajax obtains json data as undefined_AJAX related. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!