Home  >  Article  >  Web Front-end  >  How to use ajax to get json data as undefined

How to use ajax to get json data as undefined

php中世界最好的语言
php中世界最好的语言Original
2018-03-30 16:49:112157browse

This time I will show you how to use ajax to get json data that is undefined. What are the things to note when using ajax to get json data that is undefined? The following is a practical case, let's take a look.

Asynchronous

JavaScript and XML (Ajax) are key technologies driving the new generation of Web sites (popularly known as Web 2.0 sites). Ajax allows data retrieval in the background without interfering with the display and behavior of a 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 in the action as json

attribute, and in his own After the project failed to pass the test, 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. , converted into a json object, and 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 converted json object 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);
      }

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other php Chinese websites related articles!

Recommended reading:

Detailed explanation of the steps to receive josn data using josnp in ajax

##How to use an elegant solution for front-end ajax requests accomplish

The above is the detailed content of How to use ajax to get json data as undefined. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:js object propertiesNext article:js object properties