Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confused over proper way to use json

I'm new to jquery and just started reading up on json early last night. I have a working ajax call that passes simple data to a php script, works on the data and returns a single value. Works great.

I want to add the passing of arrays and non array data back and forth and after reading piles of articles saying json IS the way to go!!!! BUT... also that json isn't secure, ... put braces, dont put braces - use json decode, dont use it, make sure the top item is an object, some say put a "d" at the top, some use serialization, some don't use it. Do I have to put the curley braces around the data? Or does Jquery. Some people have said to also add a header that specifies application/json or some other header... and others have said it breaks IE7... some have said to add ...

"dataType" : json

in the ajax call. Do you see why I am confused now?

... and ... In the PHP do I have to also use rawurldecode? before json decode since my ajax urlencodes the data ... it's sooo confusing...

I don't want this to be a bigger security hole than it already is... I would like to do it PROPERLY and to SPEC. if there is a spec.

Most people thankfully say... it's about HOW you use it (jquery, JSON) etc and THAT makes it "secure". And by secure... I mean, used properly. Nothing on the client side is EVER secure.

What I'm stuck on is simple I'm sure but after reading for over 9+ hours I can't find a definitive answer. Why can't the people who make the languages (Jquery) write an example that says... here's the PROPER way to do it now that we have included json support!?!

And yes, I'm being a tad wordy in this top portion because I've asked short questions before and been downvoted and criticized and accused of not researching things or not "looking into it enough" when I NEVER ask a question here without at least 5+ hours of intense reading, scanning websites, etc.. and usually I only come here after way over 9+ hours ...

...but the information I have found is broken, old, and in too many pieces and I want to do it PERFECTLY!!! :) The best website I found so far was for pulling in flicker pictures but it didn't show how to send properly so I left there happy to find the article... but didn't know how to apply it to MY situation...

Please see the comments in my code for the tips I need please or even better - fix up my code so I can send those 2 datas back and forth and in each language can you assign a value to a tmp variable so I can SEE how you extract the information from the passed data? Thanks...

JQUERY/JS (my old code) (code snippits)

    //Here's some sample data... - HOW DO I SEND THIS AS JSON (please) back and forth?
    var myarray = new Array();
    myarray.push("One");
    myarray.push("Two");
    myarray.push("Three");
    var someOtherData = "helpmeplease";

    $.ajax({
        url: "../../ajax/ajax.php",
        type: 'POST',
        data: ({
            "testarray" : myarray,
            "somemoredata" : someOtherData
        }),
        success: function(results) {
            // what do I do with results?  please alert the 2 passed variables back from php
            // for example alert("Test:" . results[0] . results.someOtherData) or however you
            // access the returned values...

PHP RECEIVE FROM AJAX (my old test code)

     $test1 = rawurldecode($_POST["somemoredata"]);
     $test2 = rawurldecode($_POST["testarray"]);
     $test3 = testarray[0]; // should be One

PHP RETURN

     // please send any data back to the Ajax call 1 array, 1 normal data please and alert
     // the data please so I can see how it's pulled back out...
     return $data; // 1 array, 1 normal variable please

More info on my stuff...

  • my website is UTF-8 encoded

  • My data set will be under 4 megs. Most likely under 200k.

  • No cross domain stuff taking place

  • I have a nonce, per page token authentication system that works great

  • what can go in the data? ANYTHING? Any symbols, or stuff that should NOT be included that would break the JSON code or the passing of data back and forth?

  • any other tips, suggestions, warnings?

Thank you for your time.

like image 525
PerryCS Avatar asked Sep 15 '12 19:09

PerryCS


1 Answers

There is no need to send data as JSON, it would mean to include other libraries for nothing. You can use jQuery to send data as array, and read it as array in your PHP script.

Eg:

  1. Returning a single var from PHP:

    <script type="text/javascript">
    $(window).load(function(){
        var myarray = new Array();
        myarray.push("One");
        myarray.push("Two");
        myarray.push("Three");
        var someOtherData = "helpmeplease";
    
        $.ajax({
            url: "../../ajax/ajax.php",
            type: 'POST',
            data: ({
                "testarray" : myarray,
                "somemoredata" : someOtherData
            }),
            success: function(results) {
                alert(results);
            }
        });
    });
    </script>
    

    and your script:

    <?php
        $testarray = $_POST['testarray'];
        echo $testarray[0]; // prints One
        $someOtherData = $_POST['someOtherData']; // prints helpmeplease
    ?>
    
  2. Returning the data to jQuery as array and other single variables.

    You add dataType: "json" and return data as JSON:

    <script type="text/javascript">
    $(window).load(function(){
        var myarray = new Array();
        myarray.push("One");
        myarray.push("Two");
        myarray.push("Three");
        var someOtherData = "helpmeplease";
    
        $.ajax({
            url: "../../ajax/ajax.php",
            type: 'POST',
            dataType: 'json',
            data: ({
                "testarray" : myarray,
                "somemoredata" : someOtherData
            }),
            success: function(results) {
                alert(results.somemoredata); // will alert 'helpmeplease'
                alert(results.testarray[1]); // will alert 'Two'
            }
        });
    });
    </script>
    

    and script:

    <?php
        // I used the same POST fields, but it can be any other data
        $array = array();
        $array = $_POST['somemoredata'];
        $array = $_POST['testarray'];
        echo json_encode($array); 
    ?>
    
like image 79
Mihai Iorga Avatar answered Sep 29 '22 10:09

Mihai Iorga