Ajax function not defined

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jaylau
    New Member
    • May 2011
    • 2

    Ajax function not defined

    Hello! I have an Ajax script wich works well on a page, but when I use it in another page I got the error "function not defined"

    this one works well :

    Code:
    <script type="text/javascript">
    function details(var1, var2)
    {
    	var html;
    	
    	xmlhttp = new XMLHttpRequest();
    
    	xmlhttp.onreadystatechange=function()
    	{
    		h = document.getElementById("log").innerHTML;		
    		h = h + xmlhttp.readyState + " / " + xmlhttp.status + "<br/>";
    		if(xmlhttp.readyState==4 && (xmlhttp.status==200||xmlhttp.status==0))
    		{
    			html = xmlhttp.responseText;
    			document.getElementById("res").innerHTML=html;
    		}
    	}
    	xmlhttp.open("GET","detailsCity?Pays="+var1+"&Name="+var2,true);
    	xmlhttp.send();
    }
    </script>
    Code:
      <a href=""onMouseOver= "details('${CDet.res.name}','${CDet.res.cap}')" 
      			onMouseOut="details('${CDet.res.name}','${CDet.res.cap}')">
      			${CDet.res.cap}</a><br>
    But when I put it in this page (I give the entier page here after) i got the "not defined" error:


    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page 
    language="java" 
    contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    %>
    <%@ page import="java.awt.*"  %>
    <jsp:useBean id="CList" scope="session" class="fr.n7.asi.cs.model.CLBean"></jsp:useBean>    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    
    <head>
    <link rel="stylesheet" type="text/css" href="sts.css" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    
    
    
    <script type="text/javascript">
    function details(var1, var2)
    {
    	var html;
    	
    	xmlhttp = new XMLHttpRequest();
    
    	xmlhttp.onreadystatechange=function()
    	{
    		h = document.getElementById("log").innerHTML;		
    		h = h + xmlhttp.readyState + " / " + xmlhttp.status + "<br/>";
    		if(xmlhttp.readyState==4 && (xmlhttp.status==200||xmlhttp.status==0))
    		{
    			html = xmlhttp.responseText;
    			document.getElementById("res").innerHTML=html;
    		}
    	}
    	xmlhttp.open("GET","detailsCity?Pays="+var1+"&Name="+var2,true);
    	xmlhttp.send();
    }
    </script>
    
    
    
    <title>CountryList View (JSP)</title>
    </head>
    
    <body background="http://bytes.com/images/terres.jpeg">
    
    Bonjour <% out.println(session.getAttribute("nom"));%> ! <a href="deconnect">se deconnecter</a><br><br><br>
    
    <h1>LISTE DES PAYS COMMENCANT PAR PREFIXE</h1>
    
    <table border="1">
    <c:forEach var="citem" items="${CList.res}">
    <tr><td>
    <a href="details?Code=${citem.code}">${citem.name}</a><br/>
    </td></tr>
    </c:forEach>
    
    <tr><td>
      <a href=""onMouseOver= "details('aaaa','bbbb')" 
      			onMouseOut="details('aaaa','aaaa')">
      			aaaaaa</a><br>
     </td></tr>	
      			
      			
    </table>
    <h3><a href="prefCreate.jsp">retour au menu principal</a><br/></h3>
    </body>
    </html>
    do somenone see what is happening with my code ?
    thx a lot
    Last edited by Dormilich; May 22 '11, 11:04 AM.
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    In your code line 29 and 30 is useless moreover generating the errors

    And your line 34 is seeking for a object with id res

    Comment

    • jaylau
      New Member
      • May 2011
      • 2

      #3
      ok thx I modified according to your remarks but it was still not working.
      I solved the problem, actually I put both javascript scripts in the first jsp page (before I had the first jsp calling a second one, both had ajax code (two separated functions)). I am not sure it is possible to put some javascript functions in some code returned by the XMLhttpRequest objet... i have to hunt on this.

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        Yes Very much possible,
        But its little tricky
        To do that you will have to separate the javascript code and then create a new script object and the write those code to that object, or you can simply use JQuery
        as example

        Code:
        <script>
        function send_req(uri)
        {
         $.ajax(
          {
           url: "targepage?parameter=parameter", 
           type: 'POST', 
           dataType: 'html', 
           data: uri, 
           timeout: 90000,
           success: function(html, data)
               { 
        	 $("#Reply").html(html);}
        	});
        }
        </script>
        <div id='Reply'>
        </div>

        Comment

        Working...