Solved

PHP/JSON JSON.parse: unexpected character at line 1 column 1 of the JSON data

Posted on 2015-01-04
3
5,268 Views
Last Modified: 2015-01-18
I keep getting this error in Firebug.  Seems to be causing me some registration issues.  Any idea what the bad character is?

var req1 = GetHTTPObject();
req1.open('POST',url1,true);
req1.setRequestHeader("Content-type","application/x-www-form-urlencoded");
req1.send("fullname="+fullname+"&username="+username+"&lastname="+lastname+"&email="+email+"&pwd="+pwd+"&gender="+gender+"&subscription="+subscription+"&country="+country);
req1.onreadystatechange=function(){
  if(req1.readyState == 4 && req1.status == 200)  {
  //alert(req1.responseText);
    var result = JSON.parse(req1.responseText);
    var rStatus=result.success;
    var rMsg=result.msg;

Open in new window


array(11) {["full_name"]=> string(8) "asdfasdf" ["user_name"]=> string(8) "test" ["last_name"]=> string(8) "asdfasdf" ["email"]=> string(13) "test@test.com" ["password"]=> string(32) "cc03e747a6afbbcbf8be7668acfebee5" ["status"]=> string(6) "Active" ["gender"]=> string(0) "" ["is_verified"]=> string(2) "No" ["commision"]=> string(1) "0" ["country"]=> string(13) "Aland Islands" ["region"]=> string(2) "AX" }

Open in new window



Its telling me...

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
var result = JSON.parse(req1.responseText);
0
Comment
Question by:theideabulb
  • 2
3 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40530229
Are you sure you're testing the right script?  In the JS it says something like this:

req1.send("fullname="+fullname+"&username="+username+...

But in the output display it says this:

array(11) {["full_name"]=> string(8) "asdfasdf" ["user_name"]=> string(8) "test" ...

The strings fullname and full_name are not the same.  In the output display, the user_name points to "test" but "test" is not an 8 character string.

Maybe if we can get a little more complete example, we can help connect the dots.

FWIW, "unexpected characters" in JSON are almost always invalid UTF-8 characters.  JSON requires UTF-8.  Information on character sets and character collisions is available in this article.  See near the end for information about JSON.
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_11880-Unicode-PHP-and-Character-Collisions.html
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40530242
Apologies... It looks like the E-E article about Unicode has been garbled.  I have asked them to revert to a backup copy.  You still may be able to get some value out of it, but please feel encouraged to post back here with any questions!
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
ID: 40531734
greetings theideabulb, , you are using the AJAX of the native javascript with XMLHttpRequest() and use this -
       req1.open('POST',url1,true);
to open your ajax contact.
Your problem is in your server code, as you get this error message -
      SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

With HTTP JavaScript-Object-Notation, as an Object, in a server output string, the first character MUST be a    {
so you have server output that does not properly send your HTTP JSON string.

you have this in your code -
      alert(req1.responseText);
can you post here exactly what you see in the alert for the string in  req1.responseText    ?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now