4

I have an ajax call to a php file that encodes the array into a json array/object. What I am trying to do is to print the json response into a table format or an array of div's. I am stuck on how to handle the response on ajax success. Here is my ajax..

<script>
    $(document).ready(function(){
        $("#adapter").keyup(function()
        {
            var adapter = $(this).val();
            var dataString = 'searchword='+ adapter +'&format=json' ;
            if(adapter=='' || adapter < 2 )
            {
                $("#display3").hide('');        
            }
            else
            {
                $.ajax({
                    type: "POST",
                    url: "ajax/phpfile",
                    data: dataString,
                    cache: false,
                    success: function(data) 
                    {
                        var myObj = data;
                        ///NOT how to print the result and decode in html or php///
                    }
                });
            }return false; 
        });
    });
</script>

Here is the json response back from the server. I can alert the whole json response, so I know it is working on the ajax side...

{"Result":[{"ptos":{"PTOMasterID":"1","PTOMasterPart":"828B-U6805-L1CX","PTOSeriesUniqueID":"22","PTOPrice":"2715.78","PTOSeries":"82","PTOMounting":"8B","PTOTransmission":"U68","PTOSpeed":"05","PTOShifter":"L","PTOAssemblyID":"1","PTOShaftID":"C","PTOSpecialFeature":"X","PTODate":"2011-11-30 17:28:10"}},{"ptos":{"PTOMasterID":"2","PTOMasterPart":"828B-U6805-L3CX","PTOSeriesUniqueID":"22","PTOPrice":"2715.78","PTOSeries":"82","PTOMounting":"8B","PTOTransmission":"U68","PTOSpeed":"05","PTOShifter":"L","PTOAssemblyID":"3","PTOShaftID":"C","PTOSpecialFeature":"X","PTODate":"2011-11-30 17:28:10"}]}

3 Answers 3

1
$(document).ready(function(){
    $("#adapter").keyup(function()
    {
        var adapter = $(this).val();
        var dataString = 'searchword='+ adapter +'&format=json' ;
        if(adapter=='' || adapter < 2 )
        {
            $("#display3").hide('');        
        }
        else
        {
            $.ajax({
                type: "POST",
                dataType: "json", //set this to json
                url: "ajax/phpfile",
                data: dataString,
                cache: false,
                success: function(data) 
                {
                    var myObj = data;
                    ///NOT how to print the result and decode in html or php///
                    console.log(myObj); //to see the object
                }
            });
        }return false; 
    });
});

Alternatively you could use JSON2.js like so

JSON.parse(text, reviver)

JSON 2 GITHUB

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

1 Comment

How would you use this JSON.parse(text, reviver)? Am I going to place this in the ajax response success function? Also, when I declare the dataType as Json and try to alert the repsonse back from the server, I get nothing. It just says [object Object] in the alert box. Before, without the declaration I retrieved the whole repsonse.
0

You need to declare a dataType: "JSON" if you're going to return JSON data from an ajax call. Then it's just var pto_id = data.ptos.PTOMasterID and you can do a $(object).html(pto_id); to put in the value. Hope that answers the questions you were looking for.

Comments

0

Interesting question-- I found this online. It should be able to iterate over all the objects and all the properties of the objects.

http://www.openjs.com/scripts/others/dump_function_php_print_r.php

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.