返回JSON响应

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?

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 );