Ben
Ben

Reputation: 11208

jQuery ajax keeps getting into error instead of success function

I'm trying to perform an ajax-function when on a select-event. Basically I have a Google Maps API connection which gives me a dropdown list of places based on the given input. When a user selects a value from the resultlist the ajax-function must be executed. Basic task: find results in the database by the given coordinates from Google Maps API.

For some reason I can't get this to work, while the script is reached, readyState becomes 4 and status is 200. Any suggestions?

$('#SearchIn').autocomplete({
    source: // my google maps implementation (which works correctly),

    select: function(event, ui){
        $(this).autocomplete('close');
        $(this).val(ui.item.value);

        $.ajax({
            url:        ABS_BASE + 'ajax/ajax-get-results-from-location.php?data=123',
            success:    function(result) {
                            console.log('success');
                            console.log(result); 
                        },
            error:      function(jqXHR, textStatus, errorThrown) {
                            console.log(jqXHR);
                            console.log(textStatus);
                            console.log(errorThrown);
                        }
        });         
    }
});

Update; errorThrown is 'SyntaxError'. The textStatus = 'parseerror' But the only contents from the file are:

<?php
    include './ajax-base.php';

    echo 'OK I\'m here!';

The funny thing is, the responseText from jqXHR is the echo that is made.

Upvotes: -1

Views: 712

Answers (2)

Ben
Ben

Reputation: 11208

Ah I found the mistake. In my ajax-base.php file I had set a header with contenttype application/json. This caused my error.

Upvotes: 1

Adriano Carneiro
Adriano Carneiro

Reputation: 58645

I'm guessing you are missing the closing of PHP:

<?php
include './ajax-base.php';

echo 'OK I\'m here!';
?>

Upvotes: 1

Related Questions