[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?

: Unexpected token < in JSON at position 0

Posted on 2017-12-07
3
Low Priority
?
29 Views
Last Modified: 2017-12-10
<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
0
Comment
Question by:shruti A
  • 2
3 Comments
 
LVL 45

Expert Comment

by:Chris Stanyon
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
 

Author Comment

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

Expert Comment

by:Chris Stanyon
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Join & Write a Comment

There is basically two types of AJAX request in mootools. Request  and Request.HTML Request: Request is the basic XHR request class in MooTools. While not extremely useful on its own, it provides the basic functionality for both Request.HTM…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview
Suggested Courses
Course of the Month18 days, 15 hours left to enroll

834 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