I have fully functional Cordova App, written using Framework7. Works perfect. Fetches posts, connects using categories, tags etc. Allows sharing. It's on playstore : Check Here..
My problem is, for 2 things : 1. i should be able to post comment from app directly, which I am not able to using JSON POST. It says "user should be logged in..". Actually my site is setup to accept comment from anyone without login. One has to just enter email and name. Then why is this not happening from cordova app?? See my code below. You may think that it has some scripting errors, but no, it calls the json perfectly and shows me error message..
url = "http://punashcha.com/wp-json/wp/v2/"; $$.post(url + 'comments', { NONCE_KEY: '&.~OJCQN_s7*pEW(EG)-26](Eh`Fj}YP.]ZPYC2SPruX` g!/]8c>6>I|`_B[$6i', content: $$(page.container).find('#comment-content').text(), post: $$(page.container).find('#link').attr('postid') }, function (data) { me.alert("आपला अभिप्राय मिळाला. धन्यवाद!"); }, function (err, sts) { me.alert("आपला अभिप्राय रजिस्टर झाला नाही. " + sts + err.responseText); });
Am I suppose to send some header or something?
$$.getJSON(url + "posts/?per_page=10&categories=23,24" + "&_envelope&_embed", function (json) {...})
If so, then why cant i make comment in same session?? is it that $$.post & $$.getJSON use different headers?
**i am not using any JSON plugin, and prefer not to.
Hi vinay would you try to use below AjaxPost function instead of using $$.post()
function AjaxPost(url, data, success, error, async) {
$$.ajax({
url: url,
data: data,
type: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
async: async,
dataType: "json",
contentType: "application/json",
processData: false,
cache: false,
success: success,
error: error
});
};
Reply Vinay comment, You could add 'Authorization' if you like How to use Basic Auth with jQuery and AJAX? , so function above will become (include sample variable data);
var data ={'name':'vinay','framework':'framework7'};
function AjaxPost(url, data, success, error, async) {
$$.ajax({
url: url,
data: data,
type: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization' : 'authorizationdata you like'
},
async: async,
dataType: "json",
contentType: "application/json",
processData: false,
cache: false,
success: success,
error: error
});
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.