- $.get (' ajax.php tutorial ', {
- ID: $ (' #test '). attr (' value ');
- }, function (data) {
- if (data = = 1) {
- $ (' #test_div '). Remove ();
- Alert (' Delete succeeded! ');
- } else {
- Alert (' delete failed, please try again! ');
- }
- });
This is a data that was implemented in jquery and sent using the Get method, sending a value of test ID to the ajax.php page, as Ajax.php wrote:
- <?php
- if (Isset ($_get[' id ')) {
- $result = MySQL Tutorial _query (' DELETE from user WHERE id= '. $_get[' id '). '');
- echo $result;
- }
- ?>
In this case, if the deletion succeeds, it returns an affected row number, which is usually 1, but in the callback function we have the alert () test:
Ajax return value has a newline
It is obvious to see that this 1 in the Ajax callback function contains a carriage return (line break), and more than one, which seriously affects the robustness of the program, and in some cases may be an error, so we need to use the regular expression to remove it, which we can write in the callback function:
- $.get (' ajax.php ', {
- ID: $ (' #test '). attr (' value ');
- }, function (data) {
- NewData = Data.replace (/rn/g, ");
- if (NewData = = 1) {
- $ (' #test_div '). Remove ();
- Alert (' Delete succeeded! ');
- } else {
- Alert (' delete failed, please try again! ');
- }
- });
It is not difficult to see that the code used NewData = Data.replace (/rn/g, "") This sentence, we can use the regular expression "RN" to replace it with NULL, "R" is the print head from the right side back to the left, "n" Is that the print head goes down one row high (or the paper goes up a row high), so that after the replacement, we can get a clean return value, the following figure is the effect of using the clear Ajax return value:
Ajax return value has a newline