Jquery Ajax响应在Firefox上不起作用

时间:2022-10-07 18:12:13

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

这是php中的响应。我可以确认数据没问题。

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

echo json_encode($ajax_response);
exit();

Here is the code in javascript :

这是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 ?

此代码适用于Chrome和Opera。但是,此代码在Firefox上不起作用,因为php“echo”显示在Firefox而不是ajax响应上。我还尝试在javascript中放入console.debug('invoked')。与Chrome相反,Firefox中没有显示任何结果。你知道原因吗?

The response is the same into browsers tools development.

响应与浏览器工具开发相同。

Thanks

2 个解决方案

#1


0  

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

函数.preventDefault()不接受任何参数。

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

因此Firefox可能不接受这个并简单地提交表单。然而,Chrome并不真正关心并接受它。

So change

event.preventDefault(event);

Into

event.preventDefault();

That should do the trick

这应该够了吧

#2


0  

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):

您需要在PHP中使用header()函数来指定要返回到AJAX调用的响应的内容类型。尝试以下操作(在回显响应之前放置它):

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

#1


0  

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

函数.preventDefault()不接受任何参数。

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

因此Firefox可能不接受这个并简单地提交表单。然而,Chrome并不真正关心并接受它。

So change

event.preventDefault(event);

Into

event.preventDefault();

That should do the trick

这应该够了吧

#2


0  

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):

您需要在PHP中使用header()函数来指定要返回到AJAX调用的响应的内容类型。尝试以下操作(在回显响应之前放置它):

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