0

I have the following code

$.post('page.php', {prop.name: prop.hash}, function (data) {
    console.log(data);
});

Note that, prob is an object and (name,hash) are properties.

The problem in prop.cr_name, always give me this error message: SyntaxError: missing : after property id

Also I have changed the way from prop.name to prop['name'] but the problem still occur.

How to solve this problem ?

Lion King
  • 32,851
  • 25
  • 81
  • 143
  • Possible duplicate of [Using a variable for a key in a JavaScript object literal](http://stackoverflow.com/questions/2274242/using-a-variable-for-a-key-in-a-javascript-object-literal) – Kaiido Oct 15 '15 at 05:42

2 Answers2

0

You can pass data as query string

$.post('page.php', prop.name + '=' + prop.hash, function (data) {
    console.log(data);
});

or you need to create object in the following method , Using a variable for a key in a JavaScript object literal , JavaScript set object key by variable

var data={};
data[prop.name] = prop.hash;

$.post('page.php',  data , function (data) {
    console.log(data);
});

or as an array

$.post('page.php',  [{ name : prop.name, value : prop.hash }], function (data) {
    console.log(data);
});

Doc : http://api.jquery.com/jquery.ajax/

Community
  • 1
  • 1
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

create array like

var a=[{ name : prop.name, value : prop.hash }];

and pass to method like

$.post('page.php',a, function (data) {
    console.log(data);
});
Vishnu Katpure
  • 293
  • 3
  • 15