[英]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.
问题是我收到404页面未找到错误。 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. 我正在使用localhost。
For those URL's to work you should enable user-friendly permalinks. 为了使这些URL起作用,您应该启用用户友好的永久链接。 Else you should use ?json=get_nonce&controller=posts&method=create_post
否则,您应该使用?json = get_nonce&controller = posts&method = create_post
您的代码运行正常,我将状态更改为草稿数据:{nonce:data.nonce,状态:'draft',类别:'mycategory',标题:'xxxx',内容:'xxxx'},
You can use below WP REST API create post method, Reference URL for create post : https://developer.wordpress.org/rest-api/reference/posts/#create-a-post 您可以在下面的WP REST API create post方法中使用创建URL的参考URL: https : //developer.wordpress.org/rest-api/reference/posts/#create-a-post
Cookie or Nonce based Auth refer below URLs, 基于Cookie或Nonce的身份验证指的是以下网址,
http://v2.wp-api.org/guide/authentication/ https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/ http://v2.wp-api.org/guide/authentication/ https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.