0

This code works:

$(this).load($('.pageloadlabel', this).attr('href'), {category: 1}); 

This code doesn't work:

var data = '{category: 1}'; $(this).load($('.pageloadlabel', this).attr('href'), data); 

The question is, how can I make it work?

1
  • 2
    Try removing the quotes around the JSON when you assign to data. Commented Jul 28, 2010 at 17:12

4 Answers 4

2

Your data is not a Javascript object but a string, you can convert it to object by eval e.g.

data = eval('(' + data + ')'); 

but eval is considered dangerous, so better to parse string as JSON e.g.

data = JSON.parse(data) 

For JSON lib you can use json2

Sign up to request clarification or add additional context in comments.

1 Comment

I was able to solve my problem using the JSON library. Thank you.
2

It's not JSON, it's a javascript object.

var data = { category: 1 }; 

If you have a string, you would have to convert it to a object.

And notice that your string is not a valid JSON, see the link for more details.

Comments

2

Take out the quotes, the load function is expecting an object, not a string.

Comments

0

Have you tried to use eval() on data?

var data = '{category: 1}'; $(this).load($('.pageloadlabel', this).attr('href'), eval(data)); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.