Solved

Problems with Prototype and JSON parse in IE

Posted on 2008-10-27
1
1,061 Views
Last Modified: 2013-11-11
Hello Experts,

I am trying to develop a dynamically loaded section of a web page. The main content of a div is loaded via AJAX using Prototype's 1.6 library. I load a PHP page into this div, which contains a script tag at the end to call a function that will load more content into a particular div on the PHP page. The JS script uses a Prototype Ajax.Request to call another JS file which includes JSON data, all-in-all about 30 objects contained in a single array. I loop through the JSON data which just prints a little box for each JSON object: which works fine in Firefox. In IE though, it does absolutely nothing. In my debugging, I have found that the script does run, but once I try to run evalJSON() on the responseText, the function will not run anything else, and the function either never runs or never completes. IE does not return an error. I am wondering if there is some sort of file size or length issue, but I can't be for sure. I have also checked for valid JSON and checked for trailing commas, but there are none. Please help, and see bits of source code.
Here is one JSON object out of the 30 in the file:
====================================================
{
	"list" : [
		{	
			"title" : "403(b) Plans",
			"file" : "403bp.php",
			"video" : false,
			"videourl" : "",
			"article" : false,
			"articleurl" : "",
			"b1" : false,
			"b2" : false,
			"b3" : false,
			"b4" : true,
			"b5" : false
		}
       ]
}
 
 
 
 
Here is the important section of the PHP page:
===============================================
echo "<h1><strong>Reference Library</strong></h1>
     <div id=\"references\">
								
     </div>
 
<script type=\"text/javascript\">
	list_references();
</script>
";
 
Here is the function in the JS for the function call right above:
==================================================================
function list_references() {
 new Ajax.Request('referencelibraryJSON.php', {
  method:'get',
  onSuccess: function(transport) {
    references = transport.responseText.evalJSON();
    references.list.each(function(item) {
      line1 = "<div class='refbox'>" + item.title + "<br />";
      line2 = "<div class='refbook'><a href='" + item.file + "' rel='iframe' class='lightview' title='" + item.title + " :: Reference Library :: autosize: true, scrolling:true'><img src='images/book.jpg' /></a></div>";
      line3 = "<a href='b1.php' rel='ajax' class='lightview' title='Bucket One :: Reference Library :: autosize:true, scrolling:true'><img src='images/1icon.jpg' class='reficon' /></a>";
      line4 = "<a href='b2.php' rel='ajax' class='lightview' title='Bucket Two :: Reference Library :: autosize:true, scrolling:true'><img src='images/2icon.jpg' class='reficon' /></a>";
      line5 = "<a href='b3.php' rel='ajax' class='lightview' title='Bucket Three :: Reference Library :: autosize:true, scrolling:true'><img src='images/3icon.jpg' class='reficon' /></a>";
      line6 = "<a href='b4.php' rel='ajax' class='lightview' title='Bucket Four :: Reference Library :: autosize:true, scrolling:true'><img src='images/4icon.jpg' class='reficon' /></a>";
      line7 = "<a href='b5.php' rel='ajax' class='lightview' title='Bucket Five :: Reference Library :: autosize:true, scrolling:true'><img src='images/5icon.jpg' class='reficon' /></a>";
      line8 = "<a href='" + item.articleurl + "' rel='ajax' class='lightview' title='Corporate 401(k) :: Reference Library :: autosize:true, scrolling:true'><img src='images/aicon.jpg' class='reficon' /></a>";
      line9 = "<a href='" + item.videourl + "' rel='ajax' class='lightview' title='Corporate 401(k) :: Reference Library :: autosize:true, scrolling:true'><img src='images/vicon.jpg' class='reficon' /></a>";
      line10 = "</div>";
					
      inserter = line1 + "" + line2;
      if (item.b1 == true) { inserter = inserter + "" + line3; }
      if (item.b2 == true) { inserter = inserter + "" + line4; }
      if (item.b3 == true) { inserter = inserter + "" + line5; }
      if (item.b4 == true) { inserter = inserter + "" + line6; }
      if (item.b5 == true) { inserter = inserter + "" + line7; }
      if (item.article == true) { inserter = inserter + "" + line8; }
      if (item.video == true) { inserter = inserter + "" + line9; }
      inserter = inserter + "" + line10;
      $('references').insert({ 'bottom' : inserter });
    });
    Lightview.updateViews();
   }
  });
}

Open in new window

0
Comment
Question by:exalkonium
1 Comment
 
LVL 3

Accepted Solution

by:
Repus024 earned 500 total points
ID: 22814558
IE will cache get requests with the same URL.  I suspect that is what is happening here.  Append a random number to your URL and see if that helps.  Try something like the attached code snippet

Please let me know if this does not fix the situation.
Here is one JSON object out of the 30 in the file:
====================================================
{
        "list" : [
                {       
                        "title" : "403(b) Plans",
                        "file" : "403bp.php",
                        "video" : false,
                        "videourl" : "",
                        "article" : false,
                        "articleurl" : "",
                        "b1" : false,
                        "b2" : false,
                        "b3" : false,
                        "b4" : true,
                        "b5" : false
                }
       ]
}
 
 
 
 
Here is the important section of the PHP page:
===============================================
echo "<h1><strong>Reference Library</strong></h1>
     <div id=\"references\">
                                                                
     </div>
 
<script type=\"text/javascript\">
        list_references();
</script>
";
 
Here is the function in the JS for the function call right above:
==================================================================
function list_references() {
 new Ajax.Request('referencelibraryJSON.php', {
  method:'get',
  parameters: {noCacheDummy: Math.random()},
  onSuccess: function(transport) {
    references = transport.responseText.evalJSON();
    references.list.each(function(item) {
      line1 = "<div class='refbox'>" + item.title + "<br />";
      line2 = "<div class='refbook'><a href='" + item.file + "' rel='iframe' class='lightview' title='" + item.title + " :: Reference Library :: autosize: true, scrolling:true'><img src='images/book.jpg' /></a></div>";
      line3 = "<a href='b1.php' rel='ajax' class='lightview' title='Bucket One :: Reference Library :: autosize:true, scrolling:true'><img src='images/1icon.jpg' class='reficon' /></a>";
      line4 = "<a href='b2.php' rel='ajax' class='lightview' title='Bucket Two :: Reference Library :: autosize:true, scrolling:true'><img src='images/2icon.jpg' class='reficon' /></a>";
      line5 = "<a href='b3.php' rel='ajax' class='lightview' title='Bucket Three :: Reference Library :: autosize:true, scrolling:true'><img src='images/3icon.jpg' class='reficon' /></a>";
      line6 = "<a href='b4.php' rel='ajax' class='lightview' title='Bucket Four :: Reference Library :: autosize:true, scrolling:true'><img src='images/4icon.jpg' class='reficon' /></a>";
      line7 = "<a href='b5.php' rel='ajax' class='lightview' title='Bucket Five :: Reference Library :: autosize:true, scrolling:true'><img src='images/5icon.jpg' class='reficon' /></a>";
      line8 = "<a href='" + item.articleurl + "' rel='ajax' class='lightview' title='Corporate 401(k) :: Reference Library :: autosize:true, scrolling:true'><img src='images/aicon.jpg' class='reficon' /></a>";
      line9 = "<a href='" + item.videourl + "' rel='ajax' class='lightview' title='Corporate 401(k) :: Reference Library :: autosize:true, scrolling:true'><img src='images/vicon.jpg' class='reficon' /></a>";
      line10 = "</div>";
                                        
      inserter = line1 + "" + line2;
      if (item.b1 == true) { inserter = inserter + "" + line3; }
      if (item.b2 == true) { inserter = inserter + "" + line4; }
      if (item.b3 == true) { inserter = inserter + "" + line5; }
      if (item.b4 == true) { inserter = inserter + "" + line6; }
      if (item.b5 == true) { inserter = inserter + "" + line7; }
      if (item.article == true) { inserter = inserter + "" + line8; }
      if (item.video == true) { inserter = inserter + "" + line9; }
      inserter = inserter + "" + line10;
      $('references').insert({ 'bottom' : inserter });
    });
    Lightview.updateViews();
   }
  });
}

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jquery - show / hide div depending on day and time 19 46
Converting dates in JavaScript 2 32
simple javascript form validation 2 18
If condition on Html with Asp 11 13
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question