Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-01-04
3
Medium Priority
?
6,125 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 111

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 111

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 34

Accepted Solution

by:
Slick812 earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

963 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