Unexpected token illegal occurs when newline returns Json,eval in Ajax

Source: Internet
Author: User

If the user enters a newline, it is recorded as ' space ' in the data, but is not a real space.

The program foreground uses Ajax and JSON to return data binding to appear unexpected token illegal

Example:

stored in SQL as

In the ")" and "Guide", the "forest" and "pet" between the actual save is a newline character.

$.ajax ({        URL: ',        type: ' POST ',        data: {},        dataType: ' text ',        success:function (dt) {            eval ("DT = "+ dt";        },        error:function () {        }    });

Unexpected token illegal will appear at Eval. Experience tells us unexpected token illegal is some of the formatting, tags, grammar and other incorrect, incomplete

We click on the **.js in the debug browser to go in, see the JSON no problem. Don't worry, we'll copy the JSON.

Format it on the Bejson website, it's no problem. Passed.

The problem is, look closely, you will find that in the "and", "Lin" and "pet" between the \n--text line break.

It is the culprit.

OK, find the problem, we'll solve it, the method is simple

Replace in stored procedure

Your fields are varchar:REPLACE (field name, CHAR (Ten), ' <br/> ') as [alias]

your field is Text,ntext:replace (CONVERT (VARCHAR (MAX), field name), CHAR (Ten), ' <br/> ') as [alias]

Ps:
CHAR (10) line wrap key \ n
char (13) enter \ r

Unexpected token illegal occurs when newline returns Json,eval in Ajax

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.