Solved

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

Posted on 2015-01-04
3
5,137 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 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

20 Experts available now in Live!

Get 1:1 Help Now