Wandang
Wandang

Reputation: 952

How to pass a variable from javascript/jquery to java

I wanted to use data from javascript/jquery in a java class.

I get data from another server as a postresult to my servlet. I want to use this data. This data is a JSON String. The Response looks like this:

[{"id":"1","bool_m":"0","name":"Test 1"},{"id":"2","bool_m":"1","name":"Test 2"},{"id":"3","bool_m":"0","name":"Test 3"}]

How can i archive this? If i create a new java-object and pass "data" into the constructor it wont know "data".

this is what i got(this code runs as a .jsp on a liferay-server (which uses tomcat)):

<script type="text/javascript">

    function getScenarioList() {
        $.post("url",
                {
                    action : "get_scenario_list"
                })
                .done(function(data) {
                    alert("Data Loaded: "+ data);
                    <%
                        Testclass one = new Testclass(data);
                    %>
                });
    }
</script>

Upvotes: 0

Views: 1674

Answers (2)

Mark
Mark

Reputation: 18827

You cannot use Java in AJAX request.

JSP rendered on server-side like Servlet and at finish of rendering it is nothing from Java-code, only html (and javascript). And ajax runs on client-side. Also you don't have "Java" access at ajax (at client-side).

The right way is to create this Java class at server-side. You have the "url", what is behind this url? Another jsp or servlet or maybe Liferay-Action?

Upvotes: 0

Human Being
Human Being

Reputation: 8387

You can do this by jackson . Please make a google search with JACKSON .

Upvotes: 2

Related Questions