Hi - first let me start by saying that what I want to achieve is not at all malicious, but I understand how it could potentially be used this way.
- We run about 20 sites and a shop. Most of the sites contain common content (header and footer).
- The shop stores a user's cart summary in a cookie
- I have written an ASP page to write the cart summary to the browser in JSONP format. I know it's ASP, but it does the job
- I have a piece of JQuery on the other sites that polls the JSON cart summary, and updates the html of a shop link if their cart is not empty
- The code works fine in Chrome and Firefox, but not IE. If I visit the ASP page directly in IE, the contents of the cart are correct, but through AJAX, the cart is always empty (suggesting that the cookie is not available
- I'm currently using AJAX rather than getJSON after looking into potential caching issues. This has made no difference
Any ideas?
A few thoughts...
- IE is caching an empty cart. Not so, turned caching off and changed the ASP page's output. Returned code was different, but cart still empty
- When IE uses AJAX it might show up as a different browser, and hence no cookie is available. This is the one I am concentrating on at the moment, but my knowledge is limited such that I'm not sure where to look next.
My code...
On the shop
- <%
- Response.Expires = -1
- itemCount = 0
- totalPrice = "£0.00"
- cookie = Request.Cookies("CART_CONTENT")
- If cookie <> "" then
- cookieArray = Split(cookie," ")
- itemCount = Trim(cookieArray(3))
- If itemCount <> "" then itemCount = Replace(itemCount,Chr(10),"")
- totalPrice = Trim(cookieArray(1))
- If totalPrice <> "" then totalPrice = Replace(totalPrice,Chr(10),"")
- End If
- data = "{""itemCount"" : """ & itemCount & """, ""totalPrice"" : """ & totalPrice & """}"
- Response.Write Request.QueryString("jsoncallback") & "(" & data & ");"
- %>
The JQuery...
- $.ajax({
- url: 'http://myshop.url/basket_json.asp?jsoncallback=?',
- dataType: 'jsonp',
- cache: false,
- success: function(data) {
- var itemCount = data.itemCount;
- if(itemCount>0) {
- $('#shopLink').append("<strong> ("+itemCount+", "+data.totalPrice+")</strong>");
- }
- }
- });
Many apologies if I have made some very basic errors, but I'm just getting started in the whole AJAX/JSON area, and I'm not quite up to speed yet. All help very gratefully appreciated.