2013-10-02 2 views
6

양식 데이터를 수집하고이를 jquery 객체에 넣는 jquery submit 이벤트가 있습니다. jquery 객체를 가져 와서 ColdFusion 웹 서비스에 전달하여 xml 파일을 업데이트 할 수 있습니다. 나는 단지 웹 서비스에 그것을 보내고 거기서부터의 데이터를 가지고 싶은 웹 서비스로부터의 응답을 원하지 않는다.JSON을 사용하여 AJAX Post를 통해 CFC 함수에 데이터 전달

클라이언트 측/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 : 크롬에서

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

내가

그냥 물어 불을 지르고에서 내가 "예상치 못한 문자를"GET "무단"GET 나와 필요한 정보를 추가하겠습니다.

+0

이 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> 
관련 문제