1

I,m trying to access Magento session data outside Magento using Json.Json is working fine in IE but when i tried to access Magento session data using json then it does't work. Code works in FF,Chrome,Opera .. but not in IE 7

Here is my server.php file

<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::app();

 if(isset($_GET['cart_item'])){

    Mage::getSingleton('core/session', array('name'=>'frontend'));
    $_cartQty=0;
    $_cartItem='My Bag is empty';

    foreach (Mage::helper('checkout/cart')->getCart()->getItems() as $item){
        $_cartQty+=$item->getQty();
    }

    if ($_cartQty>0)
        $_cartItem='My Bag ('. $_cartQty.')';



    echo $_GET['callback'] . '('.json_encode(array('response'=>$_cartItem)).');';
}
?>

here is my client.html file

<script type="text/javascript" src="js/jquery.js"></script>

<script type="text/javascript">

function change_cart_item(){

var cartItemUrl=jQuery('#cart_item_url').val();

    jQuery.getJSON(cartItemUrl, function(json) {
        var result=json.response;
        alert(result);
        //var cartItem = jQuery(result).find('#cart_item').html();
        //jQuery("#show_cart span").html(result);               
    });
return false;
}


</script>

<input id="cart_item_url" name="cart_item_url" type="hidden" value="http://test.com/ie.php?callback=?&cart_item=cart_item" />

<input type="button" onclick="change_cart_item()"  value="Change cart item" />

The above code always return "My Bag is empty" in IE.

Ahsan
  • 331
  • 3
  • 12

2 Answers2

2

I suggest checking why IE doesn't send the cookie headers to the /ie.php script. IE must somehow evaluate the cookie path value differently.
As a workaround try implementing a regular Magento action controller that simply returns the JSON, since that is bound to receive the cookie headers by IE (otherwise the whole store front of Magento would not work with IE).
To return JSON from a action controller use:

public function jsonAction()
{
    $_cartItem = 'My Bag is empty';
    $_cartQty = Mage::helper('checkout/cart')->getItemsQty();

    if ($_cartQty > 0) {
        $_cartItem = 'My Bag ('. $_cartQty.')';
    }

    $this->getResponse()->setBody(
        $_GET['callback'] . '(' . Mage::helper('core')->jsonEncode(array('response'=>$_cartItem)).');'
    );
}
Vinai
  • 14,162
  • 2
  • 49
  • 69
  • I tried but no luck.btw ie.php and client.html both are on different server. – Ahsan Jul 03 '12 at 16:24
  • Should make no difference, as long as the cookie exists. Have you tried including the calling JS as an external from the Magento server? – Vinai Jul 03 '12 at 20:25
  • Its a cookie "privacy policy" issue.By default IE blocked the cookie of other server.If i change the setting ( enable checkbox "always allow this site to use cookie") then it works in IE.what you say ? – Ahsan Jul 04 '12 at 02:42
  • I even tried to send a p3p header but still no luck. getResponse()->clearHeaders()->setHeader('P3P: CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"')->setBody($_GET['callback'] . '(' . Mage::helper('core')->jsonEncode(array('response'=>$_cartItem)).');' ?> reference = http://stackoverflow.com/questions/389456/cookie-blocked-not-saved-in-iframe-in-internet-explorer – Ahsan Jul 04 '12 at 04:26
  • As I suggested in the first comment reply, why not add the calling JS into a external JS script and include that from the server using ` – Vinai Jul 04 '12 at 12:24
  • After setting the header in .htacces solved the IE7 problem but still IE9 and safari not working.Any idea ? Header set P3P "policyref=\"/w3c/p3p.xml\", CP=\"NOI DSP COR NID CUR ADM DEV – Ahsan Jul 06 '12 at 04:11
0

There is a bug in IE's implementation of WinInet. If you have a cookie that has a path with a filename in it, IE will not make it available via the document.cookies property in Javascript. Such a cookie should be transmitted to the server though.