What's the best way to send JavaScript array to PHP script using GET?

前端 未结 6 643
轮回少年
轮回少年 2021-01-22 13:34

I have an interactive web application powered by jQuery where users can manipulate visual objects on the screen. When done, the \"state\" of JavaScript objects should be sent to

相关标签:
6条回答
  • 2021-01-22 13:39

    first of all, if you're updating the objects state, it should be a POST. try to keep all your GET idempotent.

    second, even if you don't want to do any AJAX, JSON is still your friend. the easiest way would be to serialize your objects into JSON and send the resulting string in the POST dataload. there are many ways to decode JSON in PHP, and you're all set.

    0 讨论(0)
  • 2021-01-22 13:39

    I've used Jquery/JSON.

    var jsonArray = $.toJSON(jsArray);
    

    Then sent it via JQuery/Ajax

    0 讨论(0)
  • 2021-01-22 13:50

    json_decode() should serve you well here.

    If you simply append a string containing the JSON representation of your javascript object to your HTTP request, you can json_decode() it in PHP and have a PHP object ready to go.

    Edit: I should mention that this page has a link to a Javascript JSON stringifier, which will convert your JS objects into the necessary JSON.

    0 讨论(0)
  • 2021-01-22 13:52

    You can use this serialize function, and then unserialize it on the PHP end.

    0 讨论(0)
  • 2021-01-22 13:57

    If it's just a simple flat array you don't need to do anything fancy, as PHP has a built in feature to parse array syntax from GET/POST variable names. Rough example below.

    Javascript side:

    // Do the parameter-building however you want, I picked the short/messy way
    var arrayvalues = [1, 2, 'a'];
    var querystring = "var[]=" + arrayvalues.join("&var[]=");
    querystring += "&var[something]=abcdef";
    // querystring is now "var[]=1&var[]=2&var[]=a&var[something]=abcdef"
    

    PHP side:

    var_dump($_POST);
    // Remember to validate the data properly!
    if ( is_array($_POST['var']) ) {
        count($_POST['var']);
        echo $_POST['var']['something'];
        array_map('do_something_interesting', $_POST['var']);
    }
    
    0 讨论(0)
  • 2021-01-22 14:02

    GET or POST will probably depend on the size of your data : there is a limit on the amout of data you can pass through GET (something like 2000 bytes ; more, depending on the browser).

    There is also an other thing to take into account : if you are modifying data, you should use POST ; not GET, which is used to... get... data.

    About the format, I would really not go for anything like a custom function doing any kind of stuff like base64 : I would definitly go for JSON, which is a native Javascript notation.

    You can for instance have a look at :

    • wikipedia
    • http://www.json.org/

    There are many JS libraries that can generate JSON (probably every JS Framework can ; and there are standlone libraries like this one) ; and since PHP 5.2 there are functions in PHP to encode/decode it (see json_encode and json_decode)

    0 讨论(0)
提交回复
热议问题