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