ajax call regarding posting
-
From the code excerpt at the bottom, added for context (from a WP course I’m doing) I’ve extracted a section below I’m confused about. We’re sending the object ourNewPost to ajax. To get the post immediately from draft to published
'status': 'publish'
has been added. But how on earth is ajax interpreting the object as I can’t find anywhere which defines how the object’s properties are to be defined/named and what other properties I can use??var ourNewPost = { 'title': $(".new-note-title").val(), 'content': $(".new-note-body").val(), 'status': 'publish' }
createNote(myEventInfo){ // Here we need to fetch the actual data that's in the form fields for this note var ourNewPost = { 'title': $(".new-note-title").val(), 'content': $(".new-note-body").val(), 'status': 'publish' } // instead of using $.getJSON we use this... $.ajax({ // Here we pass the session ID, which authenticates the logged in user beforeSend: (xhr)=>{ xhr.setRequestHeader('X-WP-Nonce', mySite.nonce); }, // We access thisNote's (the li element) data attribute 'data-id'. BTW don't use 'data-id' use 'id' url: mySite.domain + '/wp-json/wp/v2/note/', type: 'POST', data: ourNewPost, // If it's successful then return to read only success: (response)=>{ console.log("Congrats"); // log the WP server response console.log(response); $(".new-note-title, .new-note-body").val(''); $('<li>Imagine real data here</li>').prependTo("#my-notes").hide().slideDown(); }, // What if it fails error: (response)=>{ console.log("It's Broken!!!"); // log the WP server response console.log(response); } }) }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘ajax call regarding posting’ is closed to new replies.