【发布时间】:2018-06-30 23:53:05
【问题描述】:
我正在尝试使用 WP REST API 将 Handsontable 中的数据发布到 WordPress。这是我尝试过的:
$('#publish').on('click',function(e){
var data = JSON.stringify(hot.getDataAtRow(0));
$.ajax({
url: 'domain.com/staging/wp-json/wp/v2/posts/',
method: 'POST',
crossDomain: true,
dataType: 'json',
contentType: 'application/json',
data: data,
beforeSend : function(xhr) {
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
success: function( data ) {
console.log( data );
},
error: function ( error ) {
console.log( error );
}
});
});
我收到了这个回复:
{"code":"empty_content","message":"内容、标题和摘录是 空。","数据":{"状态":400}}
然而,JSON.stringify(hot.getDataAtRow(0)) 的输出是这样的:
["John Doe","Sample text","publish"]
我尝试了手动设置数据的方式,它可以工作:
data: {
"title": "John Doe",
"content": "Sample text",
"status": "publish"
}
所以我的问题是: 如何从 Handsontable 获取该格式的数据?我需要设置哪个字段是标题、内容、状态、摘录等。
【问题讨论】:
标签: json ajax wordpress api handsontable