Posted: April 9, 2014 at 4:43 pm
|
Hello all, first, I’d like to say that I’m a student and I use Esspresso JSON API (v2.1.1.P) for three days, so I want you to excuse me if the answer to my question is obvious. I only want to do a POST request to modify a registration. First, I get what I want to change like this : $.get(url, function(data) { }); After, in the GET callback function, I want to modify the json and to POST it :
But I have this error : “POST and PUT requests must contain all input in a field called ‘body’. Eg: ‘body:”{\’Registrations\’:[{\’id:\’12,…}]}” I don’t understand because for me I send something like this. So : Thank you, Tony |
|
Hi Tony, I’ll be honest and say that my JSON API knowledge isn’t the best, but if I am understanding this correctly it appears that the body variables format is incorrect, in that your info has the container data rather than body. See here https://eventespresso.com/wiki/api-addon/#POST.2FPUT_.2Fregistrations |
|
Thank you for your answer. If I do this I don’t have the error : <pre class=”brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false”>$.get(url, function (data) { datas = {body : data.body}; $.post(url, datas, function (data) { }); But I have another error that I don’t understand because datas is an object : Warning: json_decode() expects parameter 1 to be string, array given in /home/kimantis/public_html/dev/wp-content/plugins/espresso-json-api/includes/helpers/EspressoAPI_Response_Formatter.class.php on line 95 Warning: Cannot modify header information – headers already sent by (output started at /home/kimantis/public_html/dev/wp-content/plugins/espresso-json-api/includes/helpers/EspressoAPI_Response_Formatter.class.php:95) in /home/kimantis/public_html/dev/wp-content/plugins/espresso-json-api/includes/helpers/EspressoAPI_Response_Formatter.class.php on line 47 |
|
So, nobody does POST request using JSON API ? I’m on it for two days now, and I’m sure a basic example will show me what I do bad. |
|
Hi, “Warning: json_decode() expects parameter 1 to be string, array given ” Well, json_decode requires a string not an array and an object is not a string. I’ll put this thread under the nose of the JSON API developer to see if he can provide a clearer example than I can. |
you almost have it right there Joey. The JSON api expects you to send a POST with an argument named ‘body’, which contains a string of JSON data. However, when you are calling jQuery.post(url,{body:body_var},callback), where body_var is a javascript object, jQuery is converting it into a multi-dimensional array of POST arguments. If you use a web-inspector (like FireBug for firefox) you can see the AJAX request going out. You were sending one similar to this: To do that, all you need to do is convert the data.body from a javascript object into a JSON string. One way to do that is to call JSON.stringify(data.body). See this example: url = "http://localhost/eetrunk31/espresso-api/v1/registrations/9gl23pxqfz.pretty_json"; jQuery.ajax(url,{ cache:true, success:function(data){ console.log(data); jQuery.post(url, {body: JSON.stringify(data.body)}, function(data2){ console.log(data2) }) } }) Here is a stack overflow discussion on other ways to convert a javascript object into JSON string, and the downside of using JSON.stringify(): http://stackoverflow.com/questions/4162749/convert-js-object-to-json-string Also, we haven’t provided any examples on how to send data to the JSON api using javascript because this is the type of thing normally only site-admins should be doing. If you’re using this javascript code from a publicly-accessible page, realize that anyone can load the page and CHANGE your javascript to do whatever they want! So make sure you’re either only using this javascript from a password-protected page, or from server-side code which isn’t visible to site visitors. |
|
|
You’re right, I finally decided to do not do that. What I’m going to do is using custom questions, and I will bring the answers by SQL requests because JSON doesn’t provide answers for attendees additional questions. It’s no longer the topic, but you can suggest your ideas to do that better. Thanks |
|
The answers table (wp_events_answer) is pretty simple. We recommend using the WPDB object to interact with it (). You’ll need the attendee’s registration_id, attendee_id, and the question_id before you do the insert. Also make sure that the question is part of a group that is assigned to additional attendees for that event. So it would look something like: $wpdb->insert( EVENTS_ANSWER_TABLE, array( 'registration_id' => $registration_id, 'attendee_id' => $attendee_id, 'question_id' => $question_id, 'answer' => $answer), array( '%s', '%d', '%d', '%s')); |
The support post ‘POST request JSON API’ is closed to new replies.
Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.