我需要有关phonegap和wordpress rest api的帮助。我对phonegap有点新意。我希望能够使用GET等接收我的最新帖子,并可能从我的手机发布,也可能执行其他CRUD操作。感谢
答案 0 :(得分:0)
我已经完成了,非常感谢。我在这里发布我的代码以帮助那里的人以及这些社区:
//设备事件监听器
document.addEventListener("deviceready", onDeviceReady, false);
var portfolioPostsContainer = document.getElementById("portfolio-posts-container");
var portfolioPostsContainer2 = document.getElementById("portfolio-posts-container2");
function onDeviceReady(){
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET', 'http://mydomain/wp-json/wp/v2/posts?_embed');
ourRequest.onload = function() {
if (ourRequest.status >= 200 && ourRequest.status < 400) {
var data = JSON.parse(ourRequest.responseText);
createHTML(data);
console.log(data);
// portfolioPostsBtn.remove();
} else {
console.log("We connected to the server, but it returned an error.");
}
};
ourRequest.onerror = function() {
console.log("Connection error");
};
ourRequest.send();
}
function createHTML(postsData) {
var ourHTMLString = '';
var postid ='';
for (i = 0; i < postsData.length; i++) {
var posturl = postsData[i].link;
ourHTMLString +='<tr>';
ourHTMLString += '<td>' + '<a href="loadpost.html?id='+postsData[i].id+'" + rel="external" + " style="text-decoration:none ">' + postsData[i].title.rendered + postsData[i].id+'</a>'+'</td>';
ourHTMLString += '<td>' + '<a href="loadpost.html?id='+postsData[i].id+'"><img width="100%" src ="' + postsData[i]._embedded['wp:featuredmedia']['0'].source_url + '" />' + '</a>'+'</td>';
ourHTMLString += '<td>' + postsData[i].excerpt.rendered + '</td>';
ourHTMLString+= '</tr>';
}
portfolioPostsContainer.innerHTML = ourHTMLString;
}