开发者

jQuery SESSION problem with Google Chrome and Safari on MAC OS x 10.5 only

I have a problem with the following code, which doesn't want to work in Chrome and Safari:

function updateBasket(data) {
if (data != '') {
    $.each(data, function(k, v) {
        if (jQuery.inArray(k, data.remove) == -1 && $('.' + k).length > 0) {
            $('.' + k).html(v);
        }
    });
}
}

$('.add_to_basket').live('click', function() {
    var button = $(this);
    var classes = $(this).attr("class");
    var identity = $(this).attr("rel").split('_');
    var url = '/basket/action/add/type/' + identity[0] + '/id/' + identity[1];
    $.ajax({
        type: 'POST',
        url: url, 
        data: ({ cls : classes }),
        dataType: 'json',
        success: function(data) {
            if (!data.error) {
                updateBasket(data);
                button.replaceWith(data.button);
            }   
        }       
    });
    return false;
});
开发者_StackOverflow社区

The .add_to_basket trigger only works with the first click - after that it doesn't do anything. It works fine with Firefox and IEs, but for some reason Chrome and Safari doesn't seem to like it.

Here's the json response which comes after first click:

{
    "error": false,
    "items": {
        "27": {
            "price": "120.00",
            "qty": 1,
            "discount": 0,
            "vat": 0,
            "price_vat": 120
        }
    },
    "bundles": [],
    "no_of_items": 1,
    "sub_total": "£120.00",
    "vat_total": "£0.00",
    "total": "£120.00",
    "remove": ["remove", "error", "button", "items", "basket"],
    "button": "<a href=\"#\" class=\"button button_red add_to_basket\" rel=\"item_27\">Remove from the basket<\/a>"
}

and the one which comes after second one (returning to the initial state):

{
    "error": false,
    "items": [],
    "bundles": [],
    "no_of_items": 0,
    "sub_total": "&pound;0.00",
    "vat_total": "&pound;0.00",
    "total": "&pound;0.00",
    "remove": ["remove", "error", "button", "items", "basket"],
    "button": "<a href=\"#\" class=\"button button_blue add_to_basket\" rel=\"item_27\">Add to the basket<\/a>"
}

Any idea what I'm doing wrong here?

To make it even difficult - this is only happening with Chrome and Safari on Mac OSX 10.5 (Leopard) - but doesn't on Snow Leopard - using the same versions of browsers (Chrome 11.0.696.71)

Just an update - I've done a few tests and it appears to be a problem with SESSIONS. It doesn't allow me to log in either - meaning session is not created when using Chrome or Safari on Mac OS X 10.5.

Any idea?


I created a simple testcase at jsfiddle. Im not sure why your example works in other browser, but it shouldn't unless you have omitted some code from the question.

Here is a working example which works in chrome. http://jsfiddle.net/ZtTeg/2/

In order to make it work, i had to add parse the returned html button into a jquery element (it seems that only then is the click handler added)

    var replacement = $(data.button)
    button.replaceWith(replacement)

Tested this with firefox and chrome, and it works here. Hope this helps.


In addition you say you have some problems with the backend (SESSION). I can't help you with that, but when mcoking/emulating the response recieved in my testcase it all seems to work as you wanted. Good luck.


Ok - I've figured out what was causing problems:

if (!isset($_SESSION)) {
     session_start();
}

which should simply be:

session_start();

as it checks on its own whether the session is set.

Thanks for participating everyone!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜