jquery Ajax program is always a problem when slicing a string returned by a PHP program

Source: Internet
Author: User
For example, the PHP program outputs a

Echo ' yt| '. $row ["Dig"];

The AJAX program slices it:
function Dig (ID) {//when clicking on a vote link
var content = document.getElementById ("Dig" +id); Get the element that says "cast a vote"
Gets the element that displays the number of votes, whose id attribute value is a number, similar to id= "3"
var dig = document.getElementById (ID);
$.ajax ({
Type: "Get",
URL: "service.php",
Data: {id:id,n:math.random ()},//Send record ID to service.asp
Beforesend:function () {$ (DIG). HTML (");},
Success:function (data) {//process the returned
R=data.split ("|");
Alert (r[0]);
if (r[0] = = "YT") {//The case has already been cast
$ (content). HTML ("You've already voted!" ");
$ (DIG). HTML (r[1]); Show the original number of votes
}
else if (data = = "NoData")//No records found
{Alert ("Parameter Error! "); }
else{
$ (DIG). HTML (data); Server modification succeeded, number of votes updated
$ (content). HTML ("vote success"); Will vote a vote for success
SetTimeout ("Rightinfo (" +id+ ")", 3000); Call Rightinfo (ID) after 3 seconds
} }}); }
It's always a shard.
After testing, the problem is that r[0] = = "YT" returns false. But with alert (r[0]), the output is YT, but there seems to be a blank line on the YT.
Before using ASP program output There is no such problem, previously:
Response.Write ("YT" & "," &rs ("Dig"))
This question can not be found on the internet, what is the reason AH


Reply to discussion (solution)

1. Verify that there are no blank lines in front of your PHP file
2. If your file is Utf-8 encoded, make sure no BOM header

For example, the PHP program outputs a

Echo ' yt| '. $row ["Dig"];

The AJAX program slices it:
function Dig (ID) {//when clicking on a vote link
var content = document.getElementById ("Dig" +id); Get the element that says "cast a vote"
Gets the element that displays the number of votes, whose id attribute value is a number, similar to id= "3"
var dig = document.getElementById (ID);
$.ajax ({
Type: "Get",
URL: "service.php",
Data: {id:id,n:math.random ()},//Send record ID to service.asp
Beforesend:function () {$ (DIG). HTML (");},
Success:function (data) {//process the returned
R=data.split ("|");
Alert (r[0]);
if (r[0] = = "YT") {//The case has already been cast
$ (content). HTML ("You've already voted!" ");
$ (DIG). HTML (r[1]); Show the original number of votes
}
else if (data = = "NoData")//No records found
{Alert ("Parameter Error! "); }
else{
$ (DIG). HTML (data); Server modification succeeded, number of votes updated
$ (content). HTML ("vote success"); Will vote a vote for success
SetTimeout ("Rightinfo (" +id+ ")", 3000); Call Rightinfo (ID) after 3 seconds
} }}); }
It's always a shard.
After testing, the problem is that r[0] = = "YT" returns false. But with alert (r[0]), the output is YT, but there seems to be a blank line on the YT.
Before using ASP program output There is no such problem, previously:
Response.Write ("YT" & "," &rs ("Dig"))
This question can not be found on the internet, what is the reason AH
Remove the front and back spaces for the returned values first
Trim (data), of course, you can also go to trim (r[0]) space

No, there is no space, I think a lot of ways, such as using the trim function to go to the space, or with intval conversion to shaping are useless. My file is gb2312 encoded, preceded by a header ("content-type:text/html; charset=gb2312 "); ,

I simply changed the ASP program's RS to PHP $row.

I think it should be caused by the problem described in the following article:

The return value of Ajax has a carriage return (also known as a newline character) workaround
Http://www.111cn.net/wy/js-ajax/39612.htm

I follow the method in the article, the results with the alert output, Firefox does not change the line, but IE6 still have, and both browsers are not working properly
if (r[0] = = "YT") judgment result is always false
But R[0] obviously is a yt ah, data type is also a string, the head of the block to break

Echo Base64_encode (file_get_contents (' http://localhoust/service.php?id=3 '));
Post the results

Moderator, the result is:
DQoNCjM4

If I run the link directly in the browser, the result is 37, but there are two rows empty and 37 is the value of $row[' dig ']

$s = ' DQoNCjM4 '; $s = base64_decode ($s); Echo bin2hex ($s);
The hexadecimal code that gets the return value: 0d0a0d0a3338
which
0d Carriage return character
0a line break

This means that your program is There are two blank lines outside of the
This file may also be in an embedded file

Oh, yes, I have two blank lines underneath the conn.php file, I've only noticed that there are no lines on it, and I'm not paying attention to the empty lines below. After deletion everything is OK, if (r[0] = = "YT") is true,

Also, the 4 floor of the kind of way to go to the empty line is actually useless.

So you can omit the end of the program file, and there will be no more empty lines.
Use JS to delete empty lines.
Data.replace (/[\r\n]+/g, "")

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