: Unexpected token < in JSON at position 0

Posted on 2017-12-07
Low Priority
Last Modified: 2017-12-21

         $(document).ready(function()  {
                    var phone1=document.getElementById('phone').value;
                    var msg=document.getElementById("message").value;
                    // alert(msg);
                    var opt=document.getElementById("opt").value;
                    var impt=document.getElementById("importSubmit").value;

                    type: 'GET',

                    url: 'http://localhost/whatsapp_message_sender/importData.php?',

                    dataType: "json",
                     //data:'foo='+ bar+'&calibri='+ nolibri,
                    data: 'importSubmit='+impt+'&phone=' +phone1+'&message='+msg+'&opt='+opt,    

                    success: function(data){

                error: function(jqXHR, textStatus, errorThrown)


                  alert("Error: "+errorThrown+" , Please try again");  




where I went wrong here  I'm getting this error  please help me to solve problem
Question by:shruti A
  • 2
LVL 45

Expert Comment

by:Chris Stanyon
ID: 42395291
Looks like your importData.php is returning invalid JSON, so we'd need to see that file or at least the output of it to give you a proper answer.

Author Comment

by:shruti A
ID: 42395297
I have uploaded my importData.php file
LVL 45

Expert Comment

by:Chris Stanyon
ID: 42395335
OK. I've not gone through your entire code, but the first problem I see is on line 8:


You are making the AJAX call using GET, not POST so line 8 will throw an error. Either change your PHP script to deal with a GET request, or change your AJAX call to make a POST request.

Soon after that you have this line:

echo $message;

So immediately, you're outputting something that isn't JSON. Later on, you do this:


Again - more output that isn't JSON

Finally, you do attempt to output some JSON which is simply:


I would strongly suggest turning on error reporting and then try loading the script page (importData.php) directly in the browser before trying to call it using AJAX. That way you will see any errors directly in your browser. Once you know that's working, then you can deal with the AJAX side of things.

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Join & Write a Comment

Today I am discussing AJAX problems in IE7 and I bet this will helps many guys out here who have problems with AJAX work. Lets start with the discovery of problem and then we will talk about its different solutions. My last two projects included …
This article discusses how to implement server side field validation and display customized error messages to the client.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses

593 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