0

I'm writing a plugin for jQuery where I need to send a little piece of data to the server, but I need to set the var name of the json object I send:

var params = {name:'var_name'}
$.post('page.php', {params.name:'the value'}, function () { /* etc. */ });

how can I do it?

2 Answers 2

6

Use the array access syntax:

var params={};
params['varName']='a value';
$.post('...', params, function() {
    ...
});
Sign up to request clarification or add additional context in comments.

Comments

0

Define the object before the $.post() call. A little something like this:

// variable somePropertyName holds the name of the property you want to set    
var myPostParams = {};
myPostParams[somePropertyName] = "some value";

$.post('page.php', myPostParams, function () { /* etc. */ });   

Note that if some of the parameters you are passing will be constant you can define them up front:

var myPostParams = {"x" : "something", "y" : "something else"};
myPostParams[someVariablePropertyName] = "another value";
myPostParams[someOtherPropertyName] = aValueInAVariable;
//etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.