bmacnaughton Posted October 23, 2015 Posted October 23, 2015 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? 2
Martijn Geerts Posted October 23, 2015 Posted October 23, 2015 Have you seen https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/core/WireHttp.php? 2
bmacnaughton Posted October 23, 2015 Author Posted October 23, 2015 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.
Martijn Geerts Posted October 23, 2015 Posted October 23, 2015 Maybe I don't get what you want, but you receive via $input->post or $input->get, Or when you need associative nested use $_POST or $_GET. (as PW $input is not multi dimensional) Communication back you could do with the WiteHttp class.
Martijn Geerts Posted October 23, 2015 Posted October 23, 2015 Now I get you... sorry. That's a nice one for a feature request. $allDataBack = json_decode(file_get_contents('php://input'));
bmacnaughton Posted October 23, 2015 Author Posted October 23, 2015 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. 1
pwFoo Posted December 27, 2015 Posted December 27, 2015 Have you seen https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/core/WireHttp.php? Mh, I build an custom module to fetch json data from an jsonapi and convert it... now I found your post and the wireDecodeJSON function... 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now