0

Maybe this is the silliest question ever, but somehow I can't figure this out myself. I have an object (javascript object) which I want to pass to my PHP script using jQuery ajax. When I try to pass in the object as it is an error is raised, this is probably because the object can't be passed as it is but is has to be serialized.

I've tried several things like $(ui.item).serialize() or ui.item.serialize() and passing it directly to the PHP script ui.item. But this doesn't work.

This is the (sub)piece of code. Any suggestions on how to serialize/pass the object ui.item to my script?

select: function(event, ui){
  $(this).autocomplete('close');
  $(this).val(ui.item.value);
  $.ajax({
    url:      ABS_BASE + 'ajax/ajax-search-set-location.php',
    data:     ui.item,
    dataType: 'json',
    type:     'POST',
    success:  function(result){ alert(result); return false; }              
  });       
}
0

1 Answer 1

5

JSON.stringify() to convert to json string. Decode with equivalent function in php.

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

3 Comments

Educational evening (I'm from Holland) Thanks! +1 Accepted. (will accept tomorrow, must leave now and I can't accept it yet.)
Just as a sidenote. .stringify() does not work in IE7. But who cares about IE7 ;)
@gearsdigital You include json.js library as fallback. Still people using IE 6 around so bad approach to ignore 7

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.