So i am developing a hybrid app using cordova and jquery mobile. I need to login to a wordpress blog site and create a new post in my hybrid app. I am using JSON Api plugin(https://wordpress.org/plugins/json-api/) to perform AJAX post. The issue is when i try to create a new post using the create_post method
,i get the following error:
POST http//some_localhost_ip/wordpress/api/create_post/? 403(Forbidden)
I have also used JSON Api user plugin (https://wordpress.org/plugins/json-api-user/) for user authentication where in i am using the generate_auth_cookie
method.
authentication-controller.js
$.ajax({
url: SERVER_URL + "/api/get_nonce/?controller=user&method=generate_auth_cookie",
type: "POST",
headers: {
'Access-Control-Allow-Headers': 'Content-Type, Accept',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS'
},
crossDomain: true,
cache: true,
success: function(result) {
if (result.status == "ok") {
var url = SERVER_URL + "/api/user/generate_auth_cookie/?"
var dataString = {};
dataString["nonce"] = result.nonce;
dataString["username"] = username;
dataString["password"] = password;
if (!checkBox.is(':checked')) {
dataString["seconds"] = SESSION_TIMEOUT;
}
dataString["insecure"] = "cool"; // remove this if SSL certificate is installed and the url is HTTPS
$.ajax({
url: url,
type: "POST",
headers: {
'Access-Control-Allow-Headers': 'Content-Type, Accept',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS'
},
data: dataString,
crossDomain: true,
cache: false,
success: function(result) {
if (result.status == "ok") {
window.localStorage.setItem("USERDATA", JSON.stringify(result.user));
window.localStorage.setItem("USERCOOKIE", result.cookie);
console
switch (result.user.role[0]) {
case "subscriber":
$.mobile.navigate("#page-subscriber-allposts");
break;
case "author":
$.mobile.navigate("#page-author-allposts");
break;
case "editor":
$.mobile.navigate("#page-editor-allposts");
break;
default:
$.mobile.navigate("#page-contributor-allposts");
break;
}
} else {
navigator.notification.alert(result.error, function doNothing() {}, "ERROR!", "OK");
}
return;
},
error: function(error) {
navigator.notification.alert("There is some issue in connecting to Authentication server", function doNothing() {}, "Breath In! Breath Out!", "Try Again");
return;
}
});
} else {
navigator.notification.alert("There is some issue in connecting to Authentication server", function doNothing() {}, "Breath In! Breath Out!", "Try Again");
return;
}
},
error: function(error) {
navigator.notification.alert("There is some issue in connecting to Authentication server", function doNothing() {}, "Breath In! Breath Out!", "Try Again");
return;
}
});
workflow-controller.js
var url = SERVER_URL;
if (null != postId && typeof postId != "undefined") {
url += "/api/get_nonce/?controller=posts&method=update_post";
} else {
url += "/api/get_nonce/?controller=posts&method=create_post";
}
$.ajax({
url: url,
type: "POST",
headers: {
'Access-Control-Allow-Headers': 'Content-Type, Accept',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS'
},
crossDomain: true,
cache: false,
success: function(result) {
alert(JSON.stringify(result));
if (result.status == "ok") {
var dataString = {};
if (null != postId && typeof postId != "undefined") {
url = SERVER_URL + "/api/update_post/?";
} else {
url = SERVER_URL + "/api/create_post/?";
dataString["post_id"] = postId;
}
dataString["nonce"] = result.nonce;
dataString["cookie"] = window.localStorage.getItem("USERCOOKIE");
dataString["author"] = author;
dataString["title"] = title;
dataString["content"] = news;
$.ajax({
url: url,
type: "POST",
headers: {
'Access-Control-Allow-Headers': 'Content-Type, Accept',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS'
},
data: dataString,
crossDomain: true,
cache: false,
success: function(result) {
alert(JSON.stringify(result));
if (result.status == "ok") {
navigator.notification.alert("Your post was successfully submitted and is pending for review", function doNothing() {}, "Hurray!!", "Ok");
$.mobile.navigate("#page-author-allposts");
return;
}else {
navigator.notification.alert("There is some issue in submitting your post", function doNothing() {}, "Breath In! Breath Out!", "Try Again");
return;
}
},
error: function(error) {
navigator.notification.alert("There is some issue in submitting your post", function doNothing() {}, "Breath In! Breath Out!", "Try Again");
return;
}
});
}else {
navigator.notification.alert("There is some issue in submitting your post", function doNothing() {}, "Breath In! Breath Out!", "Try Again");
return;
}
},
error: function(error) {
navigator.notification.alert("There is some issue in submitting your post", function doNothing() {}, "Breath In! Breath Out!", "Try Again");
return;
}
});