0

I am using AJAX to call data from a Coldfusion cfc. I am receiving the data from the cfc; however, when I try to apply the data to an object, I am getting an error in the console,

Cannot read property 'myvarname from cfc' of undefined

Here is the code that I am using:

// find value of id and name
$j(".dig3").on("click", function(e) {
    var ProgramId = '<cfoutput>#SESSION.id#</cfoutput>';
    $j.ajax({
        cache: false,
        type: 'GET',
        url: 'cfc/cfc_myCFC.cfc?method=qProgram&returnformat=json',
        dataType: "json",
        data: {
            program_id: ProgramId
        },
        success: function(data) {        
            $j("#programID").val(data[i].ProgramId); 
            $j("#mailform-input-myvarname").val(data[i].myvarname);       
        }
    });

    $j("#formName").dialog("open");
});

Am I structuring something wrong here? Any help is appreciated.

Chris Pilie
  • 451
  • 2
  • 7
  • 21
  • 1
    Your issue is the `console.log` calls at the end of the click handler. The `$.ajax` call is asynchronous, so at the point you call them `data` has no value. You need to put all code that relies on the result of the request within the `success` callback – Rory McCrossan Jul 08 '16 at 15:27
  • Appreciate the response Rory. I removed the console code. The error appears to be occurring on this line $j("#programID").val(data[i].ProgramId); . Isn't that within the success callback? – Chris Pilie Jul 08 '16 at 15:49
  • It is, yes. Could you edit your question to include the format of the returned `data` – Rory McCrossan Jul 08 '16 at 15:59
  • I have completed your request – Chris Pilie Jul 08 '16 at 16:01
  • I can't see the code in the edit? – Rory McCrossan Jul 08 '16 at 16:02
  • I edited the title and removed the console code. It should be different – Chris Pilie Jul 08 '16 at 16:52
  • I think there is a conflict with core.js When I remove it and replace it with jquery.js it works but the rest of the template breaks... – Chris Pilie Jul 09 '16 at 03:02
  • Okay... I narrowed it down to a jQuery Form Plugin. If I remove the Plugin from the core.js it works.... It is a plugin from > http://malsup.com/jquery/form/ – Chris Pilie Jul 09 '16 at 03:11

0 Answers0