The solution of return line and space in the Ajax returned value share _php tutorial

Source: Internet
Author: User
Recently write a page, with jquery Ajax to achieve judgment, just write good test completely no problem, after two days found problems, judgment is not. It was later found that all alert return values were preceded by a number of line breaks and spaces. (Still don't understand, the same computer, the same environment, why this problem)

Later on the internet found that some people have encountered the same problem. Either the jquery $.ajax $.get $.post in or the XMLHttpRequest method of the OST has this problem, so the time of invocation and judgment sometimes goes wrong. See Example

Ajax Code:

Copy the Code code as follows:
$.get (' ajax.php ', {' name ': name},function (data) {
alert (data);
})

ajax.php

Copy the Code code as follows:
$username = $_get[' name '];
echo $username;
?>

Obviously the name of the Google string in front of a line or a space, which seriously affect the robustness of the program, I have encountered problems, the following procedures are not established. So we need to use regular expressions to get rid of them, so we can write

Copy the Code code as follows:
$.get (' ajax.php ', {' name ': name},function (data) {
Newdata=data.replace (/\s/g, ");
alert (NewData);
})

Ajax.php don't change.

Then there will be no newline space.

All I see on the internet is the use of

Newdata=data.replace (/rn/g, ");

But I found that this is a problem for me, the front only reduced a part of the white space characters, so I judge the blank word character should have in addition to line carriage return the remaining whitespace characters, so the use of \s.

/.../g is a property of a regular expression that represents a full-text match, rather than finding a stop

http://www.bkjia.com/PHPjc/824875.html www.bkjia.com true http://www.bkjia.com/PHPjc/824875.html techarticle recently write a page, with jquery Ajax to achieve judgment, just write good test completely no problem, after two days found problems, judgment is not. Later found all alert come out of the return ...

  • 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.