views:

67

answers:

2

I'm trying to update the LastName field for PersonID. I can pass PersonID, but I don't know the syntax for also passing the LastName field.

$('input[name="LastName"]').live('focusout', function() {
    var PersonID = $(this).parents("tr").attr("ID");
    var LastName = $(this).val(); // todo: serialize
    $.ajax({
        url:'Remote/Person.cfc?method=UpdateLastName&returnformat=json'
        ,data:'PersonID='+PersonID
    });
    $(this).parents("td").empty().append(LastName);
});

Q: Is is something like data:{'PersonID='+PersonID,'LastName='+LastName}

Am I missing a squiggly line or a parenthesis or comma or dot or colon or semi-colon or plus sign or question mark or an apostrophe or quote or dollar sign or something?

+8  A: 

You're close! Change it just a little, no equals in there and use a colon like this:

data: {'PersonID':PersonID, 'LastName':LastName}

You can also do this if you prefer (not as safe if you get some weird characters though)

data: 'PersonID=' + PersonID + '&LastName=' + LastName
Nick Craver
OMG. OK, now for my next question: Do you know how much I love stackoverflow?
cf_PhillipSenn
+1  A: 

For complicated structures in case you add more then 2 vars:

var obj = new Object;
obj.something1 = 'something';
obj.something2 = Array();
obj.something2[1] = 'foo';
obj.something2[2] = 'bar';

//include json2.js
var objJSON =  JSON.stringify(obj);

$.ajax({
      url: "http://example.com",
      type: "GET",
      data: ({obj: objJSON , method : "UpdateLastName"}),
      contentType: "application/json; charset=utf-8",
      dataType: "html",
      success: function(msg){
         alert("Success: " + msg);
      },
      error: function(msg){
         alert("Error: " + msg);
      }
    });

On cf side just do

<cfset test = deserializeJSON(arguments.obj,true) >
zarko.susnjar
Wow zharcos, this is a fantastic answer!
cf_PhillipSenn
http://twitter.com/zharcos doesn't exist yet :)
cf_PhillipSenn
I thought I might help so I decided to give back to community :) Changed nick to this one. twitter.com/zarkosusnjar
zarko.susnjar