0

I have an array, which I send with ajax to my controller for make some actions on it. It looks like this:

jQuery:

var url = window.location.pathname.split('/');

segments = [];
segments.push( url[3], url[4], url[5] );

$.ajax({
   'data': { array : JSON.stringify(segments) },
});

And then, when I get it in my controller, I have an array with 1 element: key - "segments" and value with all values of an array segments.

What can be done for parse the array with 1 element into a new array with a few elements.

Thanks for attention and help

5
  • Can you re-word your question? Do you want to do something like segments = {part3: url[3], part4:url[4], part5:url[5]}; ? Commented Nov 3, 2014 at 15:24
  • Evilzebra, if I do something like you suggest, then I will have an object instead of an array. So then how can I convert it into array? Commented Nov 3, 2014 at 15:31
  • What server do you use? PHP? ASP.Net? Commented Nov 3, 2014 at 15:34
  • Andreas Furster, it specified in tags - php Commented Nov 3, 2014 at 15:38
  • See php.net/manual/en/function.json-decode.php Commented Nov 3, 2014 at 16:07

1 Answer 1

2

As per my understanding the solution as follows:

Html file sample.html

<html>
    <head>
        <title>Smaple</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script type="text/javascript" src="jquery-1.9.1.min.js"></script>
        <script>
            var url = window.location.pathname.split('/');

            segments = [];
            segments.push( url[3], url[4], url[5] );
            console.log(segments);
            console.log(JSON.stringify(segments));
            $.ajax({
                type: "POST",
                url: "sample.php",
                'data': { 'segments' : JSON.stringify(segments) },
                success: function() {

            }});

        </script>
    </head>
    <body>
        <div>TODO write content</div>
    </body>
</html>

And PHP file sample.php

<?php
$segments = json_decode($_POST['segments']);
print_r($segments);
exit;
?>
Sign up to request clarification or add additional context in comments.

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.