vincent kleine
vincent kleine

Reputation: 724

Passing a nested array from PHP to Java servlet

I have an array $tag which I print using:

foreach($tags as $t) {
                echo $t['token'] . "/" . $t['tag'] .  " ";
        }

How can I pass this $tag to a Java servlet so in the Java program I can use it just like in the PHP script by using a foreach and $t['token'] and $t['tag']?

I'm assuming this has to be done by using a POST method, is it also possible by using GET?

Update
Got a json_array:

$js_array = json_encode($tags);
echo "var javascript_array = ". $js_array . ";\n";

Which returns:

var javascript_array = [{"token":"test","tag":"NN"},{"token":"1","tag":"NN"}];

I'm trying to pass it to the servlet by using:

<script src="http://code.jquery.com/jquery-1.10.1.min.js">

$( document ).ready(function() {
    alert('ok');

                $.ajax({
                    url : "http://localhost:8080/first/SPARQL",
                    type: "POST",
                    data: $js_array,                  
                    dataType: "json", 
                    async: false,
                    success: function (){
                        alert( "succes");},
                        error: function(){
                            alert("false");
                        }
                });

});
</script>

However, it returns neither "succes" or "false". It is showing the "ok" alert. P.S. I'm running the java servlet via Eclipse and Tomcat 8. The php file is on my Wamp localhost. I can acces the url in my browser.
Also, it appears I can't use the $js_array in the javascript which I made in PHP, it says it's not set.

Update: In the chrome console it says:

    XMLHttpRequest cannot load http://localhost:8080/first/SPARQL.
 No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost' is therefore not allowed access.
jquery-1.10.1.min.js:6 x.ajaxTransport.sendjquery-1.10.1.min.js:6 x.extend.ajaxresultaat.php?nlquery=Dit+is+een+eerste+tekst:20 
(anonymous function)jquery-1.10.1.min.js:4 
x.Callbacks.cjquery-1.10.1.min.js:4 
x.Callbacks.p.fireWithjquery-1.10.1.min.js:4 x.extend.readyjquery-1.10.1.min.js:4 q

Added: response.addHeader("Access-Control-Allow-Origin", "*"); in the servlet. Error is gone now, but it still responds false because I can't acces the PHP $js_array inside of the javascript? Says $js_array is not set.

Upvotes: 0

Views: 410

Answers (3)

Om.
Om.

Reputation: 2682

If you want to put PHP variable value in JS object use it as mentioned below.

From:

 data: $js_array, 

To:

data: JSON.stringify(<?php echo $js_array; ?>),

Upvotes: 0

owenrb
owenrb

Reputation: 535

Since you already defined the javascript javascript_array variable at client-side

$js_array = json_encode($tags);
echo "var javascript_array = ". $js_array . ";\n";

Use it instead of the server-side variable:

        $.ajax({
            url : "http://localhost:8080/first/SPARQL",
            type: "POST",
            data: javascript_array, // updated here                 
            dataType: "json", 
            async: false,
            success: function (){
                alert( "succes");},
                error: function(){
                    alert("false");
                }
        });

Upvotes: 0

Dracony
Dracony

Reputation: 842

The easiest way would be to serialize it to JSON and deserialize it back in your servlet. In PHP use json_encode() to encode and decode with any JSON library on Java side

Upvotes: 6

Related Questions