Magento add to wishlist via ajax does not work with secure URL (SSL is installed)

I use magento to add to my wishlist via ajax it works fine, but after installing SSL on the server and creating secure magento checkout pages from the administrator. This does not give me any answer from ajax (302 Found).

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

below is the code 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.

+4
source share
4 answers

Hello Simranjeet, you can try the following: -

 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')); } } }); } 
+1
source

I found that ajaxToCart has built-in ssl functions, but if the theme developer is lazy, they may have neglected to include 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 http with https, but only if there is an element with the class use_ssl and value = 1. The developer who created my theme did not include this element, so when the ajax request points to an unprotected page that needs to be protected the ajax answer will not work in jquery.

So, for me, the quick solution was to simply add this element to my pages. I know that ssl will always be enabled on this site, so I just encoded it into my template, as shown below.

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

Once that was there, javascript took the value and made a replacement. So now this works great for me, just adding this to the template. If you are a theme developer and want users to be able to turn it on and off, you can check the settings in the administrator. Although you may find yourself in an unsafe page, it will tell you if ssl is included in the backend, which will require the addition of this fix.

I have not tested the following, but I think it will look something like this ...

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

By the way, after many years of evaluating stackoverflow solutions, I am doing my first post here. Thanks to all the contributors, the help is excellent, and I will try to return it now. :)

+1
source

Try updating the JavaScript url variable by setting _secure to true.

0
source

please try using the wish list below with your url just by changing its action.

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

I think that, in my opinion, you get a basic secure URL.

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

You will be taken to the login page. In other words,

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

Source: https://habr.com/ru/post/1502961/


All Articles