cscart_swissboy
cscart_swissboy

Reputation: 21

Jquery Ajax response doesn't work on Firefox

This is the response in php. I can confirm the datas are ok.

 $ajax_response = array(
    'product_code' => $ajax_products,
    'filter' => $ajax_filter
);

echo json_encode($ajax_response);
exit();

Here is the code in javascript :

$('#pr_category_filter').submit(function (event) {

    $.ajax({
        type: $(this).attr('method'),
        url: $(this).attr('action'),
        data: $(this).serialize(),
        dataType: 'json',
        cache: false,
        success: function (data) {

            if (data.product_code != null) {
                $('#pagination_contents').replaceWith(data.product_code);
            }

            if (data.filter != null) {
                $('#category_filter').replaceWith(data.filter);
            }
        },
        error: function (request, status, error) {
            return false;
        }
    });

    event.preventDefault(event);

});

This code works well on Chrome and Opera. However, this code doesn't work on Firefox because the php "echo" is displayed on Firefox instead of ajax response. I also tried to put a console.debug('invoked') in the javascript. No result is displayed in Firefox in contrary of Chrome. Do you know the reason ?

The response is the same into browsers tools development.

Thanks

Upvotes: 2

Views: 1342

Answers (2)

Robert Calove
Robert Calove

Reputation: 449

You need to use the header() function in PHP to specify the content-type of the response you're returning to the AJAX call. Try the following (place it before you echo the response):

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

Upvotes: 0

Peter M
Peter M

Reputation: 1059

The function .preventDefault() does not accept any arguments.

Probably Firefox therefore does not accept this and simply submits the form. Chrome however does not really care and does accept it.

So change

event.preventDefault(event);

Into

event.preventDefault();

That should do the trick

Upvotes: 1

Related Questions