How can I do a AJAX request in ColdFusion?
I have my javascript:
function getdata(){ var formElements=document.getElementById("CFForm_1").elements; var data=[]; for (var i=0; i<formElements.length; i++){ if(formElements[i].name == 'customersid') data.push({'customersid':document.getElementById("customersid").value}); if(formElements[i].name == 'customerstoid') data.push({'customerstoid':document.getElementById("customerstoid").value}); } $.ajax( { type: "get", url: "components/BillingCalc.cfc", data: { method:"ajaxGetTotalCost", data: data.join() }, dataType: "json", success: function( objResponse ){ } }); } My component:
component displayName="Calc" { remote function ajaxGetTotalCost(data){ data = deserializeJSON(arguments.data); WriteDump(data); abort; } I am getting the error: JSON parsing failure at character 2:'o' in [object Object],[object Object] Does anyone knows how to do AJAX request in CF?
data: data.join()is not how you create json. coldfusion will not be able to deserialize it as if it were json.document.getElementById()? Use jQuery to get those values.data.push( $('#customersid') )I have found this to be a great tool to put form data into json easily, github.com/macek/jquery-serialize-object