Jump to content

Handling JSON


bmacnaughton
 Share

Recommended Posts

I have a couple pages that just do JSON transactions. In them I confirm $config->ajax is TRUE and the $_SERVER['REQUEST_METHOD'] is 'POST'.

But then the only way I have found to get the JSON payload is to use file_get_content('php://input') then use either decode_json or wireDecodeJSON to return an associative array.

It would be great if the $config->ajax is set for PW to create $input->json (or possibly $input->xml if the payload is XML though I think that is less relevant).

Is there a cleaner way to handle this than what I am doing?

  • Like 2
Link to comment
Share on other sites

No, I hadn't but I just scanned through it. I don't see how it would give me the contents payload of an AJAX POST. I can see how to use this module to send a GET that returns JSON data (public function getJSON).

Maybe I'm just not getting it but how would you use this to create a page to process an AJAX POST where the payload of the request is the JSON data? The issue I'm wrestling with is how to get the payload; I have a solution but was wondering if there was a better way.

Reading back on my post I can see I wasn't as clear as I had thought - the web pages initiate JSON posts in JavaScript using JQuery's ajax function. The ProcessWire pages just handle the AJAX POST; they don't initiate them.

Link to comment
Share on other sites

Now I get you... sorry. That's a nice one for a feature request. 

$allDataBack = json_decode(file_get_contents('php://input'));

Yes, that's basically what I'm doing now except I'm using wireDecodeJSON because it will return an empty array for an empty JSON array. I don't have to special-case checks.

Yes, I thought it would be a nice feature. I spent a lot of time trying to dig the payload out of $input before I gave up and went to PHP-native code.

Thanks for looking.

  • Like 1
Link to comment
Share on other sites

  • 2 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...