开发者

JSON callback result parsing

I have to parse the JSON result but it does not work. This is what I am doing.

var pid = $("#pid").val();
      var catalog = $("#catalog_name").val();
      var qty = $("#qtys").val();
      var params = {
       productid: pid,
       catalog: catalog,
       ref: "Makegoodmedia",
       quantity: qty,
       command: "Add",
       modules: "PersonalizedAnalytics,PersonalizedBasket",
       Section: "home",
       "__lang": "en-CA",
       lang: "en-CA"
      };

      $.ajax(
      {
          url: "http://www.chapters.indigo.ca/Checkout/AddToBasket.ashx?callback=jsonresult",
          type: "GET",
          data: params,
          dataType: "jsonp",
          cache: false,
          error: function (request, status) {
         $("#_dialog1 .dialogHeader").css("display","block");
         $("#_dialog1 .dialogBodylOAD").css("display","none");
         $("#_dialog1 .dialogBody").html("<p class='Error'>Sorry But there is a Error</p>"); 
          },
          success: function (data) { 
            alert(data);
         // $.each(data, function(i,item){
          // alert(i+item);
         // });

         $("#_dialog1 .dialogHeader").css("display","block");
         $("#_dialog1 .dialogBodylOAD").css("display","none");
         $("#_dialog1 .dialogBody").html(data.jsonresult[0].PersonalizedBasket[0]);
          }        
      });

This is the final URL for the above Ajax call.

http://www.chapters.indigo.ca/Checkout/AddToBasket.ashx?callback=jsonresult&product开发者_Go百科id=9780143170099&quantity=1&catalog=books&ref=Makegoodmedia&command=Add&modules=PersonalizedAnalytics,PersonalizedBasket&Section=home&__lang=en-CA&lang=en-CA

And here I am getting the actual JSON result as

jsonresult({
 PersonalizedAnalytics: "// Analytics\ns.pageName=\"activebasket:add to basket\";\ns.server=\"204WP-9270 - BrowseOnline\";\ns.channel=\"basket\";\ns.pageType=\"\";\ns.prop1=\"\";\ns.prop2=\"\";\ns.prop3=\"\";\ns.prop4=\"\";\ns.prop5=\"\";\ns.prop6=\"\";\ns.prop7=\"\";\ns.prop8=\"\";\ns.prop9=\"\";\ns.prop10=\"\";\ns.prop11=\"\";\ns.prop12=\"\";\ns.prop13=\"\";\ns.prop14=\"\";\ns.prop15=\"\";\ns.prop16=\"\";\ns.campaign=\"\";\ns.state=\"\";\ns.zip=\"\";\ns.events=\"\";\ns.products=\"\";\ns.purchaseID=\"\";\ns.eVar1=\"\";\ns.eVar2=\"\";\ns.eVar3=\"\";\ns.eVar4=\"\";\ns.eVar5=\"\";\ns.eVar7=\"\";\ns.eVar8=\"\";\ns.eVar9=\"\";\ns.eVar10=\"\";\ns.eVar11=\"\";\ns.eVar12=\"\";\ns.eVar21=\"\";\ns.eVar22=\"\";\ns.t();",PersonalizedBasket: 
{
        "popUpHTML": '<div class="dialogHeader group"><a title="close" href="#"></a><h4>Not Added</h4></div>' +
   '  <div class="dialogBodyShoppingBagPopUp"><div class="shoppingBagPopUpError">Please note that one or more items were removed from your basket because the product is no longer sold.<br /></div>' +
            '       <p><a id="ctl00_ctl06_linkShoppingBagPopUpItem"></a>' +
   '  <br />' +
   '  </p>' +
            '       <div class="shoppingBagPopupButtons"><a id="ctl00_ctl06_linkShoppingBagPopUpCheckout" class="shoppingBagPopUpCheckout" href="http://www.chapters.indigo.ca/Checkout/ShoppingBag.html?Lang=en-CA&amp;ReferringSection=home&amp;Redirect=">Checkout Now</a>' +
            '       <a id="ctl00_ctl06_linkShoppingBagPopUpContinueShopping" class="btnContinueShopping" href="Checkout/#">Continue Shopping</a></div></div><!--[if lte IE 6.5]><iframe class="dialogIE6Fix" src="javascript:false;"></iframe><![endif]-->',
        "miniPopUpHTML": '<div id="ctl00_ctl06_myCartWidgetContainer" class="myCartWidgetContainer"><a href="#" class="myCartWidgetContainerClose"></a><div class="myCartWidgetHeader group"><h1>My Cart: 30 Items</h1><a id="ctl00_ctl06_myCartBtnCheckoutTop" class="addToCart" href="http://www.chapters.indigo.ca/Checkout/ShoppingBag.html?Lang=en-CA&amp;ReferringSection=home&amp;Redirect=">Checkout</a></div>' +
            '<div class="shoppingBagMainContent"><div class="shoppingBagHeadings group">' +
            '<div class="shoppingBagCol1">Item</div>' +
            '<div class="shoppingBagCol2">Description</div><div class="shoppingBagCol3">Qty</div>' +
            '<div class="shoppingBagCol4">Unit Price / <a href="http://www.chapters.indigo.ca/iRewards-program-information-page/irewardsInfo-art.html">Member Price</a></div>' +
            '</div><div id="shoppingBagGroupsWrapper"><div class="shoppingBagItemRow group" ID="shoppingBagItemRow"><div class="shoppingBagCol1">Crossfire<br /><a href="http://www.chapters.indigo.ca/books/Crossfire-Dick-Francis/9780718156633-item.html">View Details >></a></div><div class="shoppingBagCol2">Hardcover</div><div class="shoppingBagCol3"><input type="text" value="2" maxlength="2" /><div class="shoppingBagCol3Options"><a href="#" class="shoppingBagUpdateQtyWidget" productId="978071815663" catalog="books" origQuantity="2">Update Quantity</a><br /><a href="http://www.chapters.indigo.ca/Checkout/ShoppingBag.html?productid=978071815663&catalog=books&vid=&command=Delete&scSku=9780718156633&scTitle=Crossfire&scCatalogue=Book" class="shoppingBagRemoveWidget">Remove</a></div></div><div class="shoppingBagCol4"><span>$22.44 / <a href="http://www.chapters.indigo.ca/iRewards-program-information-page/irewardsInfo-art.html">$21.32</a></span><div class="shoppingBagItemSubTotal"><span>x2 subtotal: $44.88</span><span><a href="http://www.chapters.indigo.ca/iRewards-program-information-page/irewardsInfo-art.html">member</a> subtotal:  $42.64</span></div></div></div><div class="shoppingBagItemRow group shoppingBagItemRowAlt" ID="shoppingBagItemRow"><div class="shoppingBagCol1">Pharmacy Bath Soak Detox<br /><a href="http://www.chapters.indigo.ca/gifts/Pharmacy-Bath-Soak-Detox/609197010870-item.html">View Details >></a></div><div class="shoppingBagCol2">Gift & Paper</div><div class="shoppingBagCol3"><input type="text" value="2" maxlength="2" /><div class="shoppingBagCol3Options"><a href="#" class="shoppingBagUpdateQtyWidget" productId="60919701087" catalog="gifts" origQuantity="2">Update Quantity</a><br /><a href="http://www.chapters.indigo.ca/Checkout/ShoppingBag.html?productid=60919701087&catalog=gifts&vid=&command=Delete&scSku=609197010870&scTitle=Pharmacy+Bath+Soak+Detox&scCatalogue=Gift" class="shoppingBagRemoveWidget">Remove</a></div></div><div class="shoppingBagCol4"><span>$22.95</span><div class="shoppingBagItemSubTotal"><span>x2 subtotal: $45.90</span></div></div></div><div class="shoppingBagItemRow group" ID="shoppingBagItemRow"><div class="shoppingBagCol1">Quotable Magnet Good Friends<br /><a href="http://www.chapters.indigo.ca/gifts/Quotable-Magnet-Good-Friends/665475511750-item.html">View Details >></a></div><div class="shoppingBagCol2">Gift & Paper</div><div class="shoppingBagCol3"><input type="text" value="1" maxlength="2" /><div class="shoppingBagCol3Options"><a href="#" class="shoppingBagUpdateQtyWidget" productId="66547551175" catalog="gifts" origQuantity="1">Update Quantity</a><br /><a href="http://www.chapters.indigo.ca/Checkout/ShoppingBag.html?productid=66547551175&catalog=gifts&vid=&command=Delete&scSku=665475511750&scTitle=Quotable+Magnet+Good+Friends&scCatalogue=Gift" class="shoppingBagRemoveWidget">Remove</a></div></div><div class="shoppingBagCol4"><span>$6.99</span></div></div><div class="shoppingBagItemRow group shoppingBagItemRowAlt" ID="shoppingBagItemRow"><div class="shoppingBagCol1">Wild Ginger & Fresh Fig Bar Soap<br /><a href="http://www.chapters.indigo.ca/gifts/Wild-Ginger-Fresh-Fig-Bar/839918001584-item.html">View Details >></a></div><div class="shoppingBagCol2">Lucia</div><div class="shoppingBagCol3"><input type="text" value="10" maxlength="2" /><div class="shoppingBagCol3Options"><a href="#" class="shoppingBagUpdateQtyWidget" productId="83991800158" catalog="gifts" origQuantity="10">Update Quantity</a><br /><a href="http://www.chapters.indigo.ca/Checkout/ShoppingBag.html?productid=83991800158&catalog=gifts&vid=&command=Delete&scSku=839918001584&scTitle=Wild+Ginger+%26+Fresh+Fig+Bar+Soap&scCatalogue=Gift" class="shoppingBagRemoveWidget">Remove</a></div></div><div class="shoppingBagCol4"><span>$10.00</span><div class="shoppingBagItemSubTotal"><span>x10 subtotal: $100.00</span></div></div></div><div class="shoppingBagItemRow group" ID="shoppingBagItemRow"><div class="shoppingBagCol1">Don Cherry\'s Hockey Stories, Part…<br /><a href="http://www.chapters.indigo.ca/books/Don-Cherrys-Hockey-Stories-Part-Don-Cherry/9780385670036-item.html">View Details >></a></div><div class="shoppingBagCol2">Hardcover</div><div class="shoppingBagCol3"><input type="text" value="1" maxlength="2" /><div class="shoppingBagCol3Options"><a href="#" class="shoppingBagUpdateQtyWidget" productId="978038567003" catalog="books" origQuantity="1">Update Quantity</a><br /><a href="http://www.chapters.indigo.ca/Checkout/ShoppingBag.html?productid=978038567003&catalog=books&vid=&command=Delete&scSku=9780385670036&scTitle=Don+Cherry%27s+Hockey+Stories%2c+Part+2&scCatalogue=Book" class="shoppingBagRemoveWidget">Remove</a></div></div><div class="shoppingBagCol4"><span>$19.76 / <a href="http://www.chapters.indigo.ca/iRewards-program-information-page/irewardsInfo-art.html">$18.77</a></span></div></div><div class="shoppingBagItemRow group shoppingBagItemRowAlt" ID="shoppingBagItemRow"><div class="shoppingBagCol1">Lion Bookend<br /><a href="http://www.chapters.indigo.ca/gifts/Lion-Bookend/882709018445-item.html">View Details >></a></div><div class="shoppingBagCol2">Gift & Paper</div><div class="shoppingBagCol3"><input type="text" value="1" maxlength="2" /><div class="shoppingBagCol3Options"><a href="#" class="shoppingBagUpdateQtyWidget" productId="88270901844" catalog="gifts" origQuantity="1">Update Quantity</a><br /><a href="http://www.chapters.indigo.ca/Checkout/ShoppingBag.html?productid=88270901844&catalog=gifts&vid=&command=Delete&scSku=882709018445&scTitle=Lion+Bookend&scCatalogue=Gift" class="shoppingBagRemoveWidget">Remove</a></div></div><div class="shoppingBagCol4"><span>$29.99</span></div></div><div class="shoppingBagItemRow group" ID="shoppingBagItemRow"><div class="shoppingBagCol1">The Girl With The Dragon Tattoo<br /><a href="http://www.chapters.indigo.ca/books/Girl-With-Dragon-Tattoo-Stieg-Larsson/9780143170099-item.html">View Details >></a></div><div class="shoppingBagCol2">Paperback</div><div class="shoppingBagCol3"><input type="text" value="12" maxlength="2" /><div class="shoppingBagCol3Options"><a href="#" class="shoppingBagUpdateQtyWidget" productId="978014317009" catalog="books" origQuantity="12">Update Quantity</a><br /><a href="http://www.chapters.indigo.ca/Checkout/ShoppingBag.html?productid=978014317009&catalog=books&vid=&command=Delete&scSku=9780143170099&scTitle=The+Girl+With+The+Dragon+Tattoo&scCatalogue=Book" class="shoppingBagRemoveWidget">Remove</a></div></div><div class="shoppingBagCol4"><span>$7.42 / <a href="http://www.chapters.indigo.ca/iRewards-program-information-page/irewardsInfo-art.html">$6.75</a></span><div class="shoppingBagItemSubTotal"><span>x12 subtotal: $89.04</span><span><a href="http://www.chapters.indigo.ca/iRewards-program-information-page/irewardsInfo-art.html">member</a> subtotal:  $81.00</span></div></div></div><div class="shoppingBagItemRow group shoppingBagItemRowAlt" ID="shoppingBagItemRow"><div class="shoppingBagCol1">Kobo Wireless eReader - Onyx<br /><a href="http://www.chapters.indigo.ca/ereading/devices/Kobo-Wireless-eReader-Onyx/817866000538-item.html">View Details >></a></div><div class="shoppingBagCol2">eReading Device</div><div class="shoppingBagCol3"><input type="text" value="1" maxlength="2" /><div class="shoppingBagCol3Options"><a href="#" class="shoppingBagUpdateQtyWidget" productId="81786600053" catalog="gifts" origQuantity="1">Update Quantity</a><br /><a href="http://www.chapters.indigo.ca/Checkout/ShoppingBag.html?productid=81786600053&catalog=gifts&vid=&command=Delete&scSku=817866000538&scTitle=Kobo+Wireless+eReader+-+Onyx&scCatalogue=eReadingDevice" class="shoppingBagRemoveWidget">Remove</a></div></div><div class="shoppingBagCol4"><span>$149.00</span></div></div>' +
            '</div></div>' +
   '<div class="shoppingBagBottomSection"><div class="shoppingBagMessageArea">\u003ca href=\"/free-shipping-orders-over/promo_freeship-art.html\"\u003eFree Shipping\u003c/a\u003e on orders over $25.\u003cbr /\u003e\r\nSome restrictions apply.\r\n</div>' +
            '<a href="#" class="myCartUpdateAll">update all</a><div class="shoppingBagTotalSection"><label for="ctl00_ctl06_NewPriceText" id="ctl00_ctl06_NewPriceLabel" class="shoppingBagSubtotal">Subtotal: </label><span id="ctl00_ctl06_NewPriceText" class="shoppingBagSubtotal">$485.56</span><br />' +
   '<label for="ctl00_ctl06_myCartMemberPriceText" id="ctl00_ctl06_myCartMemberPriceLabel" class="shoppingBagMemberSubtotal"><a href="http://www.chapters.indigo.ca/iRewards-program-information-page/irewardsInfo-art.html">Member</a> Subtotal: </label><span id="ctl00_ctl06_myCartMemberPriceText" class="shoppingBagMemberSubtotal">$474.29</span><br /><div class="shoppingBagButtons group"><a id="ctl00_ctl06_myCartBtnCheckoutBottom" class="addToCart" href="http://www.chapters.indigo.ca/Checkout/ShoppingBag.html?Lang=en-CA&amp;ReferringSection=home&amp;Redirect=">Checkout</a></div></div>' +
            '</div><!--[if lte IE 6.5]><iframe class="dialogIE6Fix" src="javascript:false;"></iframe><![endif]--></div>',
        "itemCount": '30',
  "MCSCProfile": ''
}

});

How can I get the "popUpHTML" value from the output? I tried with data.jsonresult[0].popUpHTML, but it does not work. What's the problem that I am facing here?

Note : my request domain is different this jsonp call to domain is external one


Just use:

url: "http://www.chapters.indigo.ca/Checkout/AddToBasket.ashx?callback=?",

Then, jQuery will take care of the callback so you just get the object inside the parentheses. You can then access data.PersonalizedBasket.popUpHTML. I made a jsFiddle demo.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜