By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,256 Members | 1,700 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,256 IT Pros & Developers. It's quick & easy.

Comma Problem in AJAX ResponseText

P: 11
Hi,

I am attempting an ajax request. I am using PHP serverside. Below is what the response text should be.

ajaxRequest.responseText ="{username_is_taken : "This username has been taken",}"

However, the only way I can get the responseText to work on a website via an alert is to take out the comma before the curly bracket. By taking out I mean just not printing the comma with the rest of the string in PHP. If I take out this comma then it will alert fine. If I do not take the comma out then the alert just does not work.

As a last resort I have tried escaping the comma in PHP and that still does not work. Also before people say that this is JSON and having the comma before the curly bracket is not valid the responseText should still alert fine with the comma before "eval"ing or parsing the responseText.

Any help would be greatly appreciated.

Sincerely,
Ryan
Nov 15 '07 #1
Share this Question
Share on Google+
6 Replies


acoder
Expert Mod 15k+
P: 16,027
What about the double quotes? Are they escaped?
Nov 15 '07 #2

P: 11
The double quotes are escaped. Below is the code.

Expand|Select|Wrap|Line Numbers
  1. print "{";
  2. if($username == $row['username']){
  3. $username_is_taken="This username has already been taken";
  4. print "\"username_is_taken\" : \"".$username_is_taken."\",";
  5. }
  6. if($email_address == $row_two['email']){
  7. $email_is_taken="This email address has already been used";
  8. print "\"email_is_taken\" : \"".$email_is_taken."\"";
  9. }
  10. print "}";
Hi,

I am attempting an ajax request. I am using PHP serverside. Below is what the response text should be.

ajaxRequest.responseText ="{username_is_taken : "This username has been taken",}"

However, the only way I can get the responseText to work on a website via an alert is to take out the comma before the curly bracket. By taking out I mean just not printing the comma with the rest of the string in PHP. If I take out this comma then it will alert fine. If I do not take the comma out then the alert just does not work.

As a last resort I have tried escaping the comma in PHP and that still does not work. Also before people say that this is JSON and having the comma before the curly bracket is not valid the responseText should still alert fine with the comma before "eval"ing or parsing the responseText.

Any help would be greatly appreciated.

Sincerely,
Ryan
Nov 16 '07 #3

acoder
Expert Mod 15k+
P: 16,027
I can alert the same string just fine, though I haven't tested with responseText.
Nov 16 '07 #4

gits
Expert Mod 5K+
P: 5,390
hi ...

i guess the problem is with the eval of the responseText, the following:

Expand|Select|Wrap|Line Numbers
  1. alert(eval("{username_is_taken : 'This username has been taken',}"))
  2.  
produces a syntax error, while:

Expand|Select|Wrap|Line Numbers
  1. alert(eval("{username_is_taken : 'This username has been taken'}"))
  2.  
does not.

kind regards
Nov 19 '07 #5

acoder
Expert Mod 15k+
P: 16,027
He did say before eval-ing and parsing the responseText.
Nov 20 '07 #6

gits
Expert Mod 5K+
P: 5,390
hi ...

i know, i should have said that the only problem should be with the eval ... simply doing:

Expand|Select|Wrap|Line Numbers
  1. alert("{username_is_taken : 'This username has been taken',}")
shouldn't make any problem ... so we should see an explicit example of the current responseText-string ...

kind regards
Nov 20 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.