Simranjeet
Simranjeet

Reputation: 178

Magento add to wishlist via ajax is not working with secure url (SSL installed)

I am using magento add to wishlist via ajax it's working fine but after install SSL on server and make secure magento checkout pages from admin. It give me not any response from ajax (302 Found).

But if i open this url in new tab then it's working fine. When i use https in request url then it gives me the following html response "Reload the page to get source for: REQUEST URL" and without https there is no response to display.

here below the code which i used for :-

function additemtowishlist(wId,pId)
{
    var wishlisturl = 'wishlist/index/ajaxadd/product/'+pId;
    var wishlistparam = '/?wishlist_id='+wId;
    var url = '<?php echo Mage::getUrl("",array('_secure'=>false))?>'+wishlisturl+wishlistparam;
    new Ajax.Request(url, {
        dataType: "json",
        onSuccess: function(response){
            if (typeof(response.responseText) == 'string') eval('data = ' + response.responseText); 
            if (typeof data.product_id != 'undefined') {
                var htmltoshow = '<div class="messages successmessage"><div class="success-msg"><span>'+data.message+'</div></div>';
                jQuery("#wishlistresulthome-"+data.product_id).html(htmltoshow);
                jQuery("#customwishlist-"+data.product_id).css('visibility','hidden');
            }
            else {
                 alert(Translator.translate('Error happened while creating wishlist. Please try again later'));
            }
        }
    });
}

Thanks in advance.

Upvotes: 4

Views: 5758

Answers (4)

RightClick
RightClick

Reputation: 1112

I discovered that ajaxToCart has ssl functionality built into it, but if the theme developer was lazy they may have neglected to include the code that tells ajaxToCart that ssl is enabled. I found it in the following code from ajax_cart_super.js.

function ajaxToCart(url,data,mine) {
    var using_ssl = $jq('.using_ssl').attr('value');
    if(using_ssl==1) { 
        url = url.replace("http://", "https://");
    }
..
..
}

As you can see, ajaxToCart will replace the http with https, but only if there is an element with the class using_ssl and value=1. The developer who made my theme didn't include that element, so when the ajax request points to an unsecure page that should be secure, the ajax response won't work in jquery.

So for me, the quick fix was to just add this element onto my pages. I know this site will always have ssl enabled so I simply hard coded it into my template as shown below.

<input type="hidden" class="using_ssl" value="1" />

Once that was there, the javascript picked up the value and did the replacement. So now it works fine for me by just adding that into the template. If you are a theme developer and you want users to be able to switch this on and off, you may want to check against the settings in the admin. Although you may be on an insecure page, it will tell you if ssl is enabled in the backend, which would require this fix to be added.

I haven't tested the following but I think it would look something like this...

if(Mage::app()->getStore()->isCurrentlySecure()){
    echo '<input type="hidden" class="using_ssl" value="1" />';
}

BTW, after years of appreciating stackoverflow solutions, I am making my first post here. Thanks to all contributors, the help is great and I'll try to pay it back now. :)

Upvotes: 1

Ravindra Shekhawat
Ravindra Shekhawat

Reputation: 116

Hello Simranjeet you may try this :-

function additemtowishlist(wId,pId)
{
    var wishlisturl = 'wishlist/index/ajaxadd/product/'+pId;
    var wishlistparam = '/?wishlist_id='+wId;
    var url = '<?php echo Mage::getUrl("",array('_secure'=>false))?>wishlist/index/ajaxadd/product/';
    new Ajax.Request(url, {
        method: 'post',
        parameters: {'wishlist_id':wId,'product_id':pId },
        onSuccess: function(response){
            if (typeof(response.responseText) == 'string') eval('data = ' + response.responseText); 
            if (typeof data.product_id != 'undefined') {
                var htmltoshow = '<div class="messages successmessage"><div class="success-msg"><span>'+data.message+'</div></div>';
                jQuery("#wishlistresulthome-"+data.product_id).html(htmltoshow);
                jQuery("#customwishlist-"+data.product_id).css('visibility','hidden');
            }
            else {
                 alert(Translator.translate('Error happened while creating wishlist. Please try again later'));
            }
        }
    });
}

Upvotes: 1

liyakat
liyakat

Reputation: 11853

please try with below with your wishlist url just change instead of my custom action

Mage::getUrl('',array('_secure'=>true))

I think that gets you the base secure url, I believe.

Mage::getUrl('customer/account/login',array('_secure'=>true))

Will get you to the login page. In other words,

Mage::getUrl('module/controller/action',array('_secure'=>true))

Upvotes: 0

Yaroslav Lytvyn
Yaroslav Lytvyn

Reputation: 246

Try update your javascript url variable, by setting _secure to true value.

Upvotes: 0

Related Questions