2016-09-24 6 views
0

AJAX ColdFusionのテストを行っていますが、AJAXを使用してColdFusionサーバーにデータを送信する際に問題が発生しました。ColdFusion AJAX:引数で要素が定義されていません

<form method="post"> 
    <p> 
     <input type="text" id="user_name" name="name" placeholder="name" value="test" /> 
     <input type="text" id="user_email" name="email" placeholder="email" value="abc" /> 
    </p> 
    <input type="button" value="Find Out" class="ajax_form"/> 
</form> 

<p id="insert"></p> 

JS:

$(document).ready(function(){ 
    $(".ajax_form").click(function(){ 
     var that = $(this).parent(), 
      url = "ajax.cfc", 
      type = that.attr('method'), 
      test = {}; 

     that.find('[name]').each(function(index, value){ 
      var that = $(this), 
       name = that.attr('name'), 
       value = that.val(); 

      test[name] = value; 
     }); 

     console.log(test.name); 

     $.ajax({ 
      url: url, 
      type: type, 
      data: { 
       method: "testCheck", 
       name: test.name, 
       email: test.email, 
       array: test 
      }, 

      success: function (responce){ 
       document.getElementById('insert').innerHTML = responce; 
      }, 
      error: function (xhr, textStatus, errorThrown){ 
       alert(errorThrown); 
      } 
     }); 
    }); 
}); 

ajax.cfmは本当に簡単で、このようになります は、ここで私が持っているHTMLコードです。 (:「500(要素の配列を引数を未定義である)」私が取得エラー)がまたは私はそれを設定すると、デフォルト値を取得します

<cfcomponent output="false"> 
    <cffunction name="testCheck" access="remote" returnformat="plain" returnType="string" output="false"> 
     <cfargument name="name" type="string" required="false" /> 
     <cfargument name="email" type="string" required="false" /> 
     <cfargument name="array" required="false" /> 

     <cfset var string = " " /> 

     <cfif (arguments.name) eq "test"> 
      <cfset string = arguments.array/> 
     <cfelse> 
      <cfset string = "incorrect" /> 
     </cfif> 

     <cfreturn string /> 
    </cffunction> 
</cfcomponent> 

問題は、CF引数が定義されていないということです。私はこれが私が送信しているオブジェクトのデータ型に関するものだと推測していますが、私は解決策を考え出すことはできません。この場合、cfargumentとしてこのタイプのデータを使用することは可能ですか?

+0

JSオブジェクトをCFに直接渡すことはできないと思います。最初にシリアル化して文字列として渡す必要があります。次に、CF関数で逆シリアル化します。 – Alex

答えて

2

エラーは、次の理由が主な原因である。

要求をして説明するようにオブジェクトとしてarrayを送信している物体(JSON)として

  1. arraycontentTypeapplication/x-www-form-urlencoded(これは明示的に指定していないので)

    したがって、オブジェクト(場合array)名前/値のペアにシリアル化されるので、サーバは(所望されなかった)次の引数を取得するURLに付加されます:

    array[name] = 'test'

    array[email] = 'abc'

    だから、あなたは、クライアント側からのデータをシリアル化する必要があるし、このようなサーバー側でデシリアライズ:

    $.ajax({ 
         url: url, 
         type: type, 
         data: { 
          method: "testCheck", 
          name: test.name, 
          email: test.email, 
          array: JSON.stringify(test) <--- Serialize here --> 
         } 
    
         , success: function (responce){ 
          document.getElementById('insert').innerHTML = responce; 
         } 
         , error: function (xhr, textStatus, errorThrown){ 
          alert(errorThrown); 
         } 
        }); 
    
    <--- Deserialize ---> 
    <cfset local.array = deserializeJSON(arguments.array)> 
    
  2. 引数配列はデフォルト

arrayにはデフォルト値を有していないと(これは上述の理由に)要求時に渡されなかった引数を持たない

    ので、それ undefinedとなります。

+0

ありがとうございます。今や意味をなさない –

関連する問題