How can i set dynamic key for ajax post data?

I am trying to set the dynamic key in the ajax post data using jquery. My question is same as this, i have followed the same way but still facing problems. Following is my ajax function

function DisableFunction(id,section,token){
        var dataVars = {};
        dataVars[section] = token;
        dataVars['id'] = id;
     $.ajax({
        type:'POST',
        url:'post.php',
        data:JSON.stringify(dataVars),
        dataType:"json",
        ...
    });
}

and below is my ajax call html tag attribute

onclick="return DisableFunction(1,'DisableNewsfeed','a6dba37437ced2c3b07469fd6c0661f3');"

When i click on it ajax function does not posting data properly. I attached a screenshot for ajax activity.

enter image description here

Can anyone kindly guide me that where is the issue that i can fix it, i would like to appreciate if someone guide me. Thank you.

Answers 1

  • EDIT: I am not sure what id, section, and token pertain to, but in either case, the dataVars object should look something like this.


    I'm guessing you don't want to pass JSON data to the server, but are expecting it back. So, data: dataVars is what you want.

    I also modified your dataVars object a little bit.

    function DisableFunction(id, section, token){
            var dataVars = {
                token: token,
                section: section,
                id: id
            };
         $.ajax({
            type: 'POST',
            url: 'post.php',
            data: dataVars,
            dataType: 'json',
            contentType: "application/json",
            ...
        });
    }
    

Related Articles