Solved

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

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

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 110

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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

622 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