user1009749
user1009749

Reputation: 63

.ajax is not invoking a CFC file to post data

I am unable to get a jQuery .ajax call to post data to my CFC, not sure what I am missing. Tried many different corrections, nothing working.

$('.addItem').click(function(){

    //data from button clicked  

    var fType = $(this).attr('id');

    $.ajax({
        type: "post",
        url: "api.cfc",
        dataType: 'json',
        data: { method: "add", ID: fType }    
    });
)};

CFC

<cfcomponent >
      <cffunction name="add" access="remote " returntype="string">
      <cfargument name="ID" type="string" required="true" />

         <cfquery datasource="dev" name="formT">
            Insert into formMap (num, stuff)
            Values (1, #arguments.ID#)
         </cfquery>

  </cffunction>
 </cfcomponent>

Upvotes: 2

Views: 1522

Answers (1)

Chris Gessler
Chris Gessler

Reputation: 23123

I believe you need to add the method name to the URL:

$('.addItem').click(function(){ 


//data from button clicked   

var fType = $(this).attr('id'); 

$.ajax({ 
    type: "post", 
    url: "api.cfc?method=add", 
    dataType: 'json', 
    data: { ID: fType }      
)}; 

You also need to deserialize the json server side into an object/var to read the values.

<cfcomponent >     
      <cffunction name="add" access="remote " returntype="string">     
      <cfargument name="theJson" required="true" />     

        <cfset json = deserializeJson(theJson)> 

         <cfquery datasource="dev" name="formT">     
            Insert into formMap (num, stuff)     
            Values (1, #json.ID#)     
         </cfquery>     

  </cffunction>     
 </cfcomponent> 

Upvotes: 1

Related Questions