1

I keep getting the error message 'Syntax Error: unexpected token <' and a xhr.status of 200 if I run the following ajax request. Considering that I am able to email myself the variable passed from the JavaScript file to the php file it seems like that the problem occurs when the data is passed back using json to the JavaScript file.

I am relativeley new to ajax and unfortunately I am unable to solve this issue on my own since hours.

JavaScript code:

var discount ="";
$('#apply_discount').click(function(){
    discount_input = $('#discount_input').val();
    $.ajax({
        type: 'POST',
        url: 'storescripts/discount.php',
        data: {
            discount_input: discount_input,
             },
        cache: false,
        dataType: 'json',
        success: function(data){
            discount = data.discount;
            alert(discount);
        },
         error:function (xhr, ajaxOptions, thrownError){  
            alert(xhr.status);               
            alert(thrownError); 
        }           
    });     

});

php code:

<?php
include("includes/session_start.php");
include("connect_to_mysql.php"); 
$discount="";
    if (isset($_POST['discount_input'])) {
        $discount_input = $_POST['discount_input'];
        $discount_input = stripslashes($discount_input);
        $discount_input = preg_replace('/\s+/', '', $discount_input);   
        $discount = mysql_query("SELECT discount FROM discount WHERE coupon_name = '$discount_input' LIMIT 1")or die(mysql_error());    
        $discount = mysql_fetch_row($discount);
        $discount = implode('', $discount);
         mail("my@myemail",$discount,"some message","From: test@mysite.com");
         echo json_encode(array('discount' => $discount));
            }   
?>
user3403273
  • 85
  • 1
  • 7
  • 3
    Check the network panel of your browser's dev tools. Look at what is actually being received by the browser. My guess? A PHP error. – Niet the Dark Absol Jul 25 '14 at 19:21
  • 1
    No hph error is occuring – user3403273 Jul 25 '14 at 19:54
  • What is the exact response (as seen in the network panel)? – Quentin Jul 25 '14 at 19:56
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jul 25 '14 at 19:57
  • Thanks, I will update all my database APIs as soon as I find time for it. – user3403273 Jul 25 '14 at 20:57
  • Response in network panel is now 200 ok. Everything is working just fine now. thanks again. – user3403273 Jul 25 '14 at 21:29

1 Answers1

1

If you wish to return json from PHP, I believe you need to either specify contentType in the PHP file or in the ajax call:

In PHP file, before the echo json_encode(...:

header('Content-Type: application/json');

or in the ajax call, specify contentType somewhere before the success property:

contentType: 'application/json',
ron tornambe
  • 10,452
  • 7
  • 33
  • 60
  • `contentType` is the header sent to the server, specifying format of data sent to server. – Runcorn Jul 25 '14 at 19:50
  • unfortunately this hasn't caused the problem. Or at least it wasn't the only thing wich has causd it. Nontheless thanks for telling me! – user3403273 Jul 25 '14 at 19:56
  • While this is something that should be done, the code in the question has `dataType: 'json'`, so jQuery will ignore whatever Content-Type the server sends and will try to parse it as JSON anyway, so this can't be anything to do with the problem. – Quentin Jul 25 '14 at 19:58
  • Okay, you're right. Since json data is being sent to the server, shouldn't contentType be included for completeness anyway? – ron tornambe Jul 25 '14 at 20:00
  • @rontornambe — Yes, that's why I said "While this is something that should be done" – Quentin Jul 25 '14 at 20:02
  • You may get some additional information about the nature of the problem by adding alert(xhr.responseText) to you error handler. – ron tornambe Jul 25 '14 at 20:22
  • I was unable to acces my server for some minutes and now everything is working fine, which a bit strange, but as long as its working now... May I had some internal server error. Thank you so much for your help again. You guy are just awesome! – user3403273 Jul 25 '14 at 21:09
  • By specifying `dataType: 'json',` in your ajax object you're telling it to expect json back. That's why this error is so prevalent. Still not finding solutions too it. – Ben Racicot May 25 '15 at 18:40