A careless Bug,json format does not standardize the resulting AJAX error

Source: Internet
Author: User

First, the event replay

today's work encountered a strange problem, the problem was very early encountered, but did not expect so long have been planted here.

At that time, a project was being linked, because the backend did not provide a data interface, so I directly built a JSON file, and then the configuration of the URL to the JSON file, the file content is probably as follows:

// Account.json {    true,    data: [{        "1",        "Zhang xx",        "employee" ,         1    }]}

Well, a very standard JavaScript object.

Then, my Ajax code is probably as follows:

function getremotedata (URL, param, success) {    $.ajax ({        ' get ',        url:url,        data:param,        ' JSON ',        false,        function(result) {            success (result);        } ,        function() {            alert (' network error, please retry ');}}    );

Then the tragedy began.

This code, go straight to the error callback

What's the reason? I started the long road of troubleshooting.

At first, I think the Ajax code is wrong, look carefully, it seems that there is no problem.

then, because I use the local JSON file caused by the problem, so I always feel that the local file is a problem.

It suddenly occurred to me that the browser has a security limit for local file access, such as chrome has this limit, you need to add parameters when starting.

but the impression that the console will be prompted, so it should not be the case,

but I'm still a dead horse. As a live horse medicine, try to see, the browser has been added to the startup parameters
--allow-file-access-from-files

The result is really useless.

Open the browser's network, troubleshoot, and find a strange phenomenonlook at the data in previewHow could my success:true become undefined:true. What the hell is this? Then the idea shifted to the JSON file direction.  then again, would it be that the data returned is not JSON-led? (In fact, this time it's close to the right answer),but I looked at the papers and didn't find any problems,so guess the way the browser treats my JSON file as a text file, and I datatype write JSON to cause parsing errors? (Cry!!!) It was supposed to be a brain-pumping. then modify the AJAX code
 function   Getremotedata (URL, param,        Success) {$.ajax ({type:  ' get '  ' text ', //  changed to text  CAC He: false   success:  function   ' + result + ') '); //  use Eval to parse a bit   "error:  Span style= "color: #0000ff;"        >function   ' network error, retry ' 
sure enough, the operation was successful. However, this is still a temporary solution to the root, but also can not switch the interface every time you change the code bar. then, take a deep breath and decide to calm down and think about the problem.  first look at their own previous code, found that it is so written, absolutely no problem. not superstitious, look at the colleague's code, the wording is not the same, but the general is the same, no problem. What the hell is the problem, the collapse Ah!  rage, open StackOverflow, start searchingSearch Ajax, local file, always error, and so on because of the wrong direction. I can only say that at that time my heart is broken, although in the process of searching, learned a lot of other knowledge (various problems link seems to see, finally unexpectedly see about react things go, time is such passing away ...) ), but the point is I still haven't solved the problem.  According to experience, often the most unreasonable cause of the problem is often the simplest, thought it must be a small mistake, but where is the error? Finally, Kung Fu is a conscientious, I found, because the JSON file format is wrong. On the jquery API website, you see this sentence.
In
jquery 1.4, JSON-formatted data is parsed in a strict way, and if there is a malformed format, JQuery is rejected and throws an exception that parses the error. (See json.org For more information, the correct JSON format.) )
So, I changed the file.
// Account.json {    true,    "date": [{        "id": "1",        "name": "Zhang xx",         "Job": "Employee",        "type": 1    }]}
well, it worked. I wonder if you see the difference between the documents. Standard JSON, all keys, is required in quotation marks. is such a small problem! two, standard JSON formatAlthough the problem solved, but this experience let me a little aftermath feeling, did so many years front end, altogether even a JSON can not grasp? It cannot be justified. so into the JSON official website (http://json.org/json-zh.html), the screen half of the content of a good look. all say the details of the devil, has been the subconscious of the JavaScript object as JSON I, this time really good tutorial on the JSON knowledge. There are a few points to share with you. 1, the object key must use double quotation marks. This is the problem that I have encountered today, not much to say. 2. The value of an object can have the following values.     Basically, there's no difference between JavaScript objects. But one thing to note here is that there is no undefined. Which means
{    "success": undefined}

Such a JSON, is wrong.

3. For number type, the method represented is as follows

When using scientific notation, it will be involved.

31 Impressions.

looking back at this bug is really very, very basic. It would have been all right when it was settled, but this time it left me in a deep meditation. Remember before someone has teased, is that the front end is too thin, CSS and JavaScript are separated, is not to be divided into a JSON engineer AH. Although only a joke, but I think most of the front end for JSON is holding a "Oh, is a JavaScript object" attitude, and not to take a serious look at its definition. recall the last two years of learning and contact front-end knowledge, a variety of engineering tools, various mv* framework, front-end application architecture model. And those basics have really been out of focus for a long time. In fact, I have always felt that the foundation is very good, from 11 onwards, bubble blue ideal forum, html,css step by step, but also practical. It also reminds of Dawn Ling, arguing about the front-end foundation and emerging new technology issues. Although I cannot say that he fully endorses his point of view, but now it is quite understandable. It's time to calm down and regain the most fundamental things in the front.

Reproduced this site article please indicate the author and source Yo rhubarb –http://www.cnblogs.com/season-huang/, do not use for any commercial use

A careless Bug,json format does not standardize the resulting AJAX error

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.