2

I'm pulling in XML data and I need to return it to the front end as JSON.

I'm processing user input via AJAX like this:

$.ajax({ url: '/process.php',
         data: {category: 'sportingGoods'},
         type: 'post',
         success: function(output) {
                      console.log(output);
                  }
});

This is sent to process.php

if(isset($_POST['category']) && !empty($_POST['category'])){
    $category = $_POST['category'];
    //echo $category;

    switch($category){
        case 'automotive': 
            $amazon->automotive; 
            break;
        case 'sportingGoods': 
            echo $amazon->sportingGoods(); 
            break;

    }
}

Then, I access a method in a class. In this case, $amazon->sportingGoods()

Now, sportingGoods() does a bunch of stuff, then returns a result set like this:

    $xml = simplexml_load_file($signedUrl);
    $json = json_encode($xml);
    $products = json_decode($json,TRUE);
    return $products;

The XML and JSON data is there. I can print_r($products) and print_r($xml) and see the results in the console. However, when I try and return it, I don't get anything. So, am I not returning an object? How can I gain access to the result?

10
  • You need to echo out the $products Commented Jun 12, 2015 at 23:42
  • @Maxxi doesn't work. I just get Array Commented Jun 12, 2015 at 23:43
  • Why 2 uses of json_encode() in sportingGoods()? jQuery will likely decode one round, but you're probably left needing to decode the other -- console.log($.parseJSON(output));. Commented Jun 12, 2015 at 23:43
  • Sorry I didn't notice you were decoding your JSON object before echoing it. You need to echo out the JSON object. Then as Jonathan says, parse the JSON with $.parseJSON() in the client Commented Jun 12, 2015 at 23:45
  • using json_encode you should also add to your ajax request that: dataType: "json", so that you can work with objects. In any case, return isn't enough, you have echo what you want to send to the client. Commented Jun 12, 2015 at 23:45

1 Answer 1

1

You need to echo out your json and then stop the script. From there, you'll use parseJSON to convert the json response into a js array.

Try this:

Your $.ajax({:

success: function( output ) {
    var response = $.parseJSON( output );
    // do something with your js array
},

and in sportingGoods():

return json_encode( $xml );
Sign up to request clarification or add additional context in comments.

5 Comments

There already is an echo made from the return of that function (sportingGoods), though not for automotive.
Thanks! That helps. I'm now seeing the object in the console.
Welcome. Don't forget to use die() or exit() after you echo the response
@Jon Is your point that I should be able to return a result from sportingGoods and echo it with the switch? That was my intention at first. Not sure why that doesn't work.
@PaulDessert Yes, though, you return the decoded json (ie a php array) from your function, and when you just echo an array, all you will get is Array, so you should have your function return the json_encode'd version. ie return $json; rather than return $products;

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.