Tasos Tasos -4 years ago 56
PHP Question

JSON API on Wordpress - creating a post

I try to create a form on my website where a visitor will be able to make a post in a specific category. The problem is that I get a 404 page not found error. Here is my code:

echo "<textarea cols='50' rows='10' style='font-size: 24px;'></textarea><br><br>";
echo "<button id='sendmessage' style='padding:10px'>Submit</button>";

echo "<script>
jQuery('#sendmessage').click(function(e){
e.preventDefault();
jQuery.ajax({
// get the nonce
dataType: 'jsonp',
url: '/api/get_nonce/?controller=posts&method=create_post',
type: 'GET',
success: function (data) {
// create the post
jQuery.ajax({
url: '/api/create_post/',
type: 'POST',
dataType: 'jsonp',
data: {nonce: data.nonce, status:'publish', categories:'mycategory', title:'xxxx', content:'xxxx'},
success: function (data) {

},
error: function (data) {
console.log('error');
}
});
},
error: function (data) {
console.log('error');
}
});
});
</script>"


On the console, I get this error:

"NetworkError: 404 Not Found - http://localhost/api/get_nonce/?controller=posts&method=create_post&callback=jQuery111109654319724222027_1423235015042&_=1423235015043"


I am working on localhost right now.

Answer Source

For those URL's to work you should enable user-friendly permalinks. Else you should use ?json=get_nonce&controller=posts&method=create_post

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download