views:

359

answers:

2

Hi, I am new to JavaScript and here is a problem when I trying out prototype.

I want to update sample.jsp with Ajax.updater after the it is loaded, but it doesn't work. Here the source of smaple.jsp.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script src="prototype.js"></script>
        <script>
            function f1(){
                var ajax = new Ajax.updater(
                {success: 'state'},'part.html'
                ,{method:'get'});
            }
            document.observe('dom:loaded', function() {
                f1();
            });

        </script>
    </head>
    <body>
        state:
        <div id="state"></div>
        <br>
    </body>
</html>

Could anyone tell me what's wrong with my code?

+1  A: 

try "Ajax.Updater" (capital U) for starters

also I recommend that you try working with firefox and the firebug plugin, it's a great way to debug your javascript

Yonatan Karni
@Yonatan, still failed after changed the "updater" to "Updater".Thanks for your advice, I've installed firebug and I am able to set break point in the JavaScript code lines, but I don't know how to debug in my case, could you please tell how to do it?
eric2323223
@eric2323223: do you see the GET requests in the firebug console?you might also want to use live http headers plugin (https://addons.mozilla.org/en-US/firefox/addon/3829) in case you don't. try to see if the request is being sent. (or check by debugging the server / inspecting the log)
Yonatan Karni
@Yonatan, yes, I saw the GET request in the firebug console. What should I do next?
eric2323223
@eric2323223: great. you can inspect the content of the reply (see tabs in the console's GET request row), make sure it's not empty.if not, you can also try to change the syntax (even though it seems fine) to new Ajax.Updater('state', 'part.html');
Yonatan Karni
@Yonatan, I saw the content part.html in the response tab, does it means that the correct result has already returned? what should I do the identify the source of my problem? I've change the url to several different page and no one could return the result to my state div.
eric2323223
@eric2323223: if you see the content from the server in the firebug tab, that indeed indicates it was successfully retrieved from the server. try returning a fixed simple string such as "hello world", and if passing the object name ('state') doesn't work for you, consider passing the DOM Object, like this $('state') (the dollar function received an object's ID string and returns the object with that ID)
Yonatan Karni
@Yonatan, still have same problem after tried what you suggested. Thanks anyway.
eric2323223
A: 

I have tried another one and it works

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;

<html>
    <head>
        <title>AJAX Zip Checker </title>
        <link rel="stylesheet" href="style.css" type="text/css" />
        <script src="prototype.js"></script>
        <script type="text/javascript" language="JavaScript">
            function checkZip() {
                if($F('zip').length == 5) {
                    var url = 'checkzip.jsp';
                    var params = 'zip=' + $F('zip');
                    var ajax = new Ajax.Updater(
                    {success: 'zipResult'},
                    url,
                    {method: 'get', parameters: params, onFailure: reportError});
                }
            }
            function reportError(request) {
                $F('zipResult') = "Error";
            }
        </script>
    </head>
    <body>

        <label for="zip">zip:</label>
        <input type="text" name="zip" id="zip" onkeyup="checkZip();" />
        <div id="zipResult"></div><p/>

    </body>
</html>

checkzip.jsp

<%
        String zip = request.getParameter("zip");
        if (zip.equals("10009")) {
%>
new york
<%} else {%>
unknown
<% }%>

Could anyone tell me the difference?

eric2323223