: Unexpected token < in JSON at position 0

<script>

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

                    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){
                    //alert(data);
                    $('#lkct').html(data);
                   

                                              },
                error: function(jqXHR, textStatus, errorThrown)

                {

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

                }      

      });

        });
});
   //});
</script>


where I went wrong here  I'm getting this error  please help me to solve problem
shruti Aweb developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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.
0
shruti Aweb developerAuthor Commented:
I have uploaded my importData.php file
importData.php
0
Chris StanyonWebDevCommented:
OK. I've not gone through your entire code, but the first problem I see is on line 8:

if($_POST['importSubmit'])

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:

print($res);

Again - more output that isn't JSON

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

"successfully"

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.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.