簡體   English   中英

從JavaScript錯誤調用公共Java方法

[英]Call public Java method from javascript error

在我的Django網頁中,我需要Javascript才能調用Java小程序的公共方法。 我想使Java Applet將一些數據發布到DJango Web服務器,但是要做到這一點,我需要Cookie來驗證會話。 考慮到這一點,我發現可以從JS調用applet的公共方法,因此我嘗試了一下,但失敗了。 我不知道這是問題所在,還是我錯過了什么。

這是代碼。

HTML:

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.1.min.js"></script>

<applet name="mapGenerator"
    code="MapGenerator.class"
    archive="{{STATIC_URL}}java/MapGenerator.jar"
    width= 500 height = 300>

        <param name=numero_immagini value="{{floor.count}}">
        <param name=id value="{{building.id}}">
        {% for f in floor %}
            <param name=immagine{{forloop.counter0}} value="{{MEDIA_URL}}{{f.link.name}}">
            <param name=numero_piano{{forloop.counter0}} value="{{f.numero_di_piano}}">
        {%endfor%}
</applet>

<script type="text/javascript">
    function initialize() {
        document.mapGenerator.setCookie(document.cookie);
    }
</script>

<body onload="initialize()">

MapGenerator類:

public class MapGenerator extends JApplet {

    //... variables

    public void init() {
        numero_immagini = Integer.parseInt(this.getParameter(N_IMMAGINI));
        id_building = Integer.parseInt(this.getParameter(ID_BUILDING));
        images = new BufferedImage[numero_immagini];
        floors = new int[numero_immagini];

        for(int i=0; i< numero_immagini; i++) {
            try {
                URL url = new URL(getCodeBase(), this.getParameter(IMMAGINE+i));
                    images[i] = ImageIO.read(url);
                    floors[i] = Integer.parseInt(this.getParameter(PIANO_IMMAGINE+i));
            } catch (IOException ioe) {ioe.printStackTrace();}
        }
    }

    public void setCookie(String cookie) {
        this.cookie = cookie;
        ed.cookie = cookie;
        System.err.println(cookie);
    }

    public void start() {

        ed = new Editor(this.getContentPane(), images, floors, id_building);                     
        this.add(ed.getPanel());
        Toolkit kit = this.getToolkit();
        Dimension dim = kit.getScreenSize();
        this.setBounds(dim.width/4, dim.height/4, dim.width/4, dim.height/4);
        this.setVisible(true);
        this.repaint();
    }
}

當我打開此網頁時,這是JS控制台中的錯誤:

Uncaught TypeError: Object #<HTMLAppletElement> has no method 'setCookie'

謝謝

我不敢相信那真是太愚蠢了...

我只需要放腳本

<script type="text/javascript">
    function initialize() {
        document.mapGenerator.setCookie(document.cookie);
    }
</script>

小程序之前...我要哭了...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM