Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
5,995 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
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.

704 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