0

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?

  1. I am using a membership plugin, that protects some of my paid posts. So from mobile app, i divert users to "website login" and get then logged in on mobile app browser. It maintains the state. Once this is done, it allows me to fetch complete protected posts. This means that my getJSON function sends proper authentication headers for mobile logged in user to fetch full data from wordpress.. The function call is -

$$.getJSON(url + "posts/?per_page=10&categories=23,24" + "&_envelope&_embed", function (json) {...})
If my categories 23, 24 are protected then i dont see complete articles, if i am not logged in. But if i am logged in, i can see complete articles. Means this getJSON sends proper headers for user..

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.

1 Answers1

0

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
    });
};
prahata
  • 26
  • 3