0

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.

2
  • have you tried the success:function(data){ console.log(data) } it will show you whether you are getting any data within return or not Commented Mar 25, 2015 at 6:23
  • what is the value of $js_array ?? Commented Mar 28, 2015 at 19:46

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

9 Comments

I tried using json_encode but it doesn't seem to work
What is the problem with it? it doesn't encode your data?
Use php.net/manual/en/function.json-last-error.php to check if there were any errors during encode
Thanks, got the json array working now. Only the passing isn't working
What do you mean by "the passing" ?
|
0

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");
                }
        });

Comments

0

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; ?>),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.