Home >Web Front-end >JS Tutorial >How to use ajax to get json data as undefined
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.
AsynchronousJavaScript 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);
var json =(new Function("","return "+data))();
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
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!