/    Sign up×
Community /Pin to ProfileBookmark

Problem testing responseText item value

I’m trying to test the value of an element returned as part of the responseText array from an Ajax call. If I simply do alert(response[8]);, I get the value of the field, which is “yes”. However, if I do the following:

if(response[8]==”yes”){
alert(“It’s true”);
} else {
alert(“It’s false”);
}

the alert always shows “It’s false”, even when the alert above shows a value of “yes”. Nothing I do seems to compel this if check to read anything in that response[8] element. I am truly stumped. Any suggestions? Am I missing something obvious?

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMay 15.2006 — To see what is going on, run this:

[CODE]var A= response[8];
var str= A.constructor;

if(str==String){ // String is not quoted!
alert('Response is the string '+response[8]);
else alert('Response is an '+str+' that needs to be converted to a string');
}[/CODE]
Copy linkTweet thisAlerts:
@LogicianMay 15.2006 — I'm trying to test the value of an element returned as part of the responseText array from an Ajax call. If I simply do alert(response[8]);, I get the value of the field, which is "yes". However, if I do the following:

if(response[8]=="yes"){

alert("It's true");

} else {

alert("It's false");

}

the alert always shows "It's false", even when the alert above shows a value of "yes". Nothing I do seems to compel this if check to read anything in that response[8] element. I am truly stumped. Any suggestions? Am I missing something obvious?[/QUOTE]
Are you sure it's 'yes' and not 'Yes' or 'YES'?
Copy linkTweet thisAlerts:
@phpnoviceMay 15.2006 — Do this:

alert("response[8] (" + response[8].length + ") = '" + response[8] + "'");
Copy linkTweet thisAlerts:
@ht1815authorMay 15.2006 — This is what I got:

"Response is the string

yes"

This lead me to the problem. Note that the "yes" wrapped to the second line. That suggested that for some reason, it had a carriage return or something in front of it. So, I did a substring(response[8],response[8].length) and that removed the carriage return. Next, I noticed that it appeared to have a space in front of it, so I used a function I had and did trim(substring(response[8],response[8].length) and got a value that I could test in an if statement successfully.

I don't understand where the carriage return - if that's what it was - came from. I guess I'll have to trim and substring all my responses. I've never seen anything about this in anything I've read on Ajax so far.

Well, I guess I'm going now. Thank you. If you know anything about why this is behaving as it is, I'd be very interested.
Copy linkTweet thisAlerts:
@phpnoviceMay 15.2006 — This is not something that Ajax did. Ajax will just pass on what it receives.
Copy linkTweet thisAlerts:
@ht1815authorMay 15.2006 — Yep, it was me. I'm providing these values from some php code, running a query against a database. As a matter of habit, I wrote out the values of the query like this:

print("

$ary[value1],

$ary[value2],

$ary[value3]");

When I viewed that as a php file, it gave me a single line of values, comma-separated. In the javascript, I was splitting the responseText by the comma. I didn't realize that the carriage returns I used in the php file to put the $ary variables on separate lines were being passed back. Once I knew there was a carriage return, it didn't take me long to figure out where they were coming from, though. I rewrite the php to write the $ary values in a single line and voila, no more carriage returns.

Man, what a chinese fire drill.
Copy linkTweet thisAlerts:
@phpnoviceMay 15.2006 — All's well that ends well. ?

Cheers.
×

Success!

Help @ht1815 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...