2012-05-06 9 views
2

私はCFCにデータを投稿するためのjQuery .ajax呼び出しを取得できません。多くの異なる訂正を試みましたが、何も動作しません。.ajaxがデータを投稿するためにCFCファイルを呼び出さない

$('.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 }  
    }); 
)}; 

私はあなたがURLにメソッド名を追加する必要があると考えている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> 
+1

デバッグには何をしましたか? Fireboxやコンソールにxhr要求が記録されていますか? –

+0

add()メソッドはreturntype = "string"を定義していますが、文字列を返しません。これによりエラーが発生する可能性があります。メソッドが何も返す必要がない場合は、returntype = "void"に変更してください。 – azawaza

答えて

1

$('.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 }  
)}; 

あなたはまたにオブジェクトの/ varにJSONサーバ側をデシリアライズする必要があります値を読んでください。

<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> 
+0

私はurlとdataの両方でメソッドを持つことを試みましたが、どちらも動作しません。 – user1009749

+0

この質問を見てください - http://stackoverflow.com/questions/999501/invoke-coldfusion-function-using-ajax –

+0

これは非常に簡単で動作しないはずです。私はRailoを使用していて、CFCファイルを探している場所かどうか疑問に思っています。 CFMだけでテストしたいのであれば、コンポーネントタグを削除してURLを.cfmに変更するだけですか? – user1009749

関連する問題