The Ajax return value has a carriage return line, the space solution method sharing _php Skill

Source: Internet
Author: User

Recently in writing a page, using jquery ajax to achieve judgment, just write a good test completely no problem, after two days to find problems, not judge. It was later found that all alert return values were preceded by a number of newline and spaces. (Still not understand, the same computer, the same environment, why the problem arises)

Later on the internet found that people have encountered the same problem. Either the jquery $.ajax $.get $.post or the XMLHttpRequest method of the original sound has this problem, and sometimes it is wrong to call and judge. See examples

Ajax Code:

Copy Code code as follows:

$.get (' ajax.php ', {' name ': name},function (data) {
alert (data);
})

ajax.php

Copy Code code as follows:

<?php
$username = $_get[' name '];
echo $username;
?>

Obviously name for Google's string before there is a line or space, such a serious impact on the robustness of the program, I have encountered problems, the following procedures are not established. So we need to get rid of the regular expression method, so we can write

Copy 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 is no newline space.

All I see on the Internet is using

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 the rest of the remaining white line, so select the \s.

/.../g is the property of a regular expression that represents the full text match, rather than finding one, stopping

Related Article

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.