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