The return value of the Ajax has a carriage return (also known as a newline character) solution

Source: Internet
Author: User
Tags mysql tutorial php tutorial

  1. $.get (' ajax.php tutorial ', {
  2. ID: $ (' #test '). attr (' value ');
  3. }, function (data) {
  4. if (data = = 1) {
  5. $ (' #test_div '). Remove ();
  6. Alert (' Delete succeeded! ');
  7. } else {
  8. Alert (' delete failed, please try again! ');
  9. }
  10. });
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:
  1. <?php
  2. if (Isset ($_get[' id ')) {
  3. $result = MySQL Tutorial _query (' DELETE from user WHERE id= '. $_get[' id '). '');
  4. echo $result;
  5. }
  6. ?>
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:
  1. $.get (' ajax.php ', {
  2. ID: $ (' #test '). attr (' value ');
  3. }, function (data) {
  4. NewData = Data.replace (/rn/g, ");
  5. if (NewData = = 1) {
  6. $ (' #test_div '). Remove ();
  7. Alert (' Delete succeeded! ');
  8. } else {
  9. Alert (' delete failed, please try again! ');
  10. }
  11. });
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

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.