2013-10-02 18 views
6

フォームデータを収集してjqueryオブジェクトに配置するjquery submitイベントがあります。そのjqueryオブジェクトを取得し、それをxmlファイルの更新に使用できるcoldfusion Webサービスに渡したいと思います。私はWebサービスからの応答を望んでいません。ただ、Webサービスに送信し、そこからのデータを使用したいと思います。JSONを使用してCFC関数にAJAXポスト経由でデータを渡す

クライアント側/ jQueryの:

$("#update").on('submit',function() { 
    $linkName = $('#update').find('#linkName').val(); 
    $linkURL = $('#update').find('#linkURL').val(); 
    $linkInfo = $('#update').find('#linkDesc').val(); 
    $numOfLinks = $('.linkSection').length; 
    if ($numOfLinks > 0){ 
    // Here the sub link names and urls put into an array 
     $subLinkName = []; 
     $subLinkURL = []; 
     $('.linkSection').each(function(index, element) { 
      $subLinkName.push($(this).find('#subLinkName').attr('value')); 
      $subLinkURL.push($(this).find('#subLinkURL').attr('value')); 

      $data = {linkName: $linkName, linkURL: $linkURL, linkID : $linkID, linkDescription : $linkInfo, subLinkNames : $subLinkName, subLinkURLs : $subLinkURL}; 
     }); 
    // Optionally, you could put the name and url in the array object here but not sure which is better to do 
     //$subLink =[]; 
     //$('.linkSection').each(function(index, element) { 
      //$subLink.push($(this).find('#subLinkName').attr('value')); 
      //$subLink.push($(this).find('#subLinkURL').attr('value')); 
     //}); 
    }else{ 
     alert('hey'); 
     $data = {linkName: $linkName, linkURL: $linkURL, linkID : $linkID, linkDescription : $linkInfo}; 
    } 
    //alert($data); 
    $.ajax({ 
     type: "POST", 
     data: { 
      method: "UpdateRegularLink",    
      returnFormat:"json",    
      formData: JSON.stringify($data) 
     }, 
     url: "../../WebServices/RMSI/rmsi.cfc", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     beforeSend: function() {      
      alert('about to post'); 
     }, 
     error: function(data,status,error){ 
      alert(data+': '+status+': '+error); 
     }, 
     done: function(data){ 
      alert('success'); 
     } 
    }); 
}); 

サーバーサイド/ CFC:私は "無許可" を取得Chromeの場合

<cfcomponent> 

    <cfset xmlpath = "e:\webapps\NRCNewsApps\RMSI\xml" /> 

    <cffunction name="UpdateRegularLink" access="remote" output="false" > 
    <cfargument name="formData" required="true" type="string" /> 
    <cfset var cfStruct = DeserializeJSON(arguments.formData)> 

    <!--- now I want to use the data ---> 
</cffunction> 

</cfcomponent> 

放火犯で 私は "予期しない文字" を取得

だけ頼みます私とあなたに必要な情報を追加します。

+0

は、あなたのCFCが呼び出されているか、エラーがAJAX呼び出しの前に起こっているCFC? –

+1

IMOのURLは "../../WebServices/RMSI/rmsi.cfc?method=UpdateRegularLink"であり、メソッドはデータから削除する必要があります。 URLがCFC拡張子で終わる場合、CFCエクスプローラがこの問題を引き起こしている可能性のある関数メタデータのHTMLを返して返します。 –

+1

cfcexplorerが承認を求めている可能性があります。これを確認するには、開発者ツール>ネットワークでcfcexplorerが呼び出されたかどうかを確認します。 –

答えて

4

したがって、coldfusionに渡すデータを文字列化すると、coldfusionはそれを理解せず、文字列にすべての種類の文字を追加して、coldfusionによって読み取り不可能にします。

JSONパケットがJSON値として解析される前にJSONパケットが文字列に変換される必要があるバイト配列(バイナリデータ)として渡されるため、仲介メソッド呼び出しとしてtoString()を使用する必要がありました。

また、データを渡すのではなく、URLの最後にメソッドを追加するために、@ Chandan Kumarを呼び出すことをおすすめします。私は実際にその部分の上にひっくり返す保管しかし、それは最終的にそれはあなたにそうKUDOSを働いたか

var ajaxResponse = $.ajax({ 
         type: "POST", 
         url: "../../WebServices/RMSI/rmsi.cfc?method=UpdateRegularLinkmethod=, 
         contentType: "application/json; charset=utf-8", 
         data: JSON.stringify($data), 
         //dataType: "json", 
         beforeSend: function() {      
          //alert($data); 
         }, 
         error: function(data,status,error){ 
          alert(data+': '+status+': '+error); 
         } 
        }).done(function(entry) { 
         alert('success'); 
        }); 


        ajaxResponse.then(
         function(apiResponse){ 

         // Dump HTML to page for debugging. 
         $("#response").html(apiResponse); 

         } 
        ); 

<cfcomponent> 
    <cffunction name="UpdateRegularLink" access="remote" returntype="xml"> 

    <cfset requestBody = toString(getHttpRequestData().content) /> 

    <!--- Double-check to make sure it's a JSON value. ---> 
    <cfif isJSON(requestBody)> 

     <!--- Echo back POST data. ---> 
     <cfdump 
      var="#deserializeJSON(requestBody)#" 
      label="HTTP Body" 
     /> 

    </cfif> 


    </cffunction> 
</cfcomponent> 
関連する問題