Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that there are carriage return line feeds and spaces in the return values of ajax

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to solve the problem of carriage return line feeds and spaces in ajax return values". The explanation in the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the problem of carriage return line feeds and spaces in ajax return values".

Recently, I was writing a page, using jquery ajax to realize the judgment. I just finished writing the test. There was no problem at all. After two days, I found that there was a problem, and the judgment failed. It turns out that all return values from alert are preceded by line breaks and spaces. (I still don't understand why there is such a problem on the same computer and in the same environment.)

Later, it was found on the Internet that some people had encountered the same problem. This problem exists both in jquery $. Ajax $. Get $. Post or in the soundtrack's XMLHttpRequest method, which sometimes makes mistakes when calling and judging. Look at the example.

Ajax Code:

The copy code is as follows:

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

Alert (data)

})

Ajax.php

The copy code is as follows:

Obviously, the string whose name is google is preceded by line breaks or spaces, which seriously affects the robustness of the program. I have encountered problems, and the judgment of the following program is not valid. So we need to remove it with regular expressions, so we can write like this.

The copy code is as follows:

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

NewData=data.replace (/\ s _

Alert (newData)

})

Ajax.php does not need to change

Then there is no wrapping space.

Everything I see on the Internet is used

NewData=data.replace (/ rn/g,'')

But I found that this rule has a problem with mine. Only part of the white space character has been reduced, so I decided that there should be other white space characters in the white space character except the newline return, so I chose\ s.

/. / g is an attribute of a regular expression that indicates a full-text match rather than stopping as soon as one is found

Thank you for your reading, the above is "how to solve the ajax return value in the carriage return line feed, space" content, after the study of this article, I believe you on how to solve the ajax return value in the carriage return line feed, space this problem has a more profound experience, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report