2013-10-02 8 views
6

Ho un evento jquery submit che raccoglie i dati del modulo e li inserisce in un oggetto jquery. Voglio prendere quell'oggetto jQuery e passarlo a un servizio web coldfusion dove posso usarlo per aggiornare un file xml. Non voglio una risposta dal servizio web Voglio solo inviarlo al servizio web e giocherellare con i dati da lì.Passare i dati alla funzione CFC con JSON tramite AJAX Post

lato client/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'); 
     } 
    }); 
}); 

Server Side/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> 

In Chrome ottengo "non autorizzato" In Firebug ho "personaggio inaspettato"

Basta chiedere io e io aggiungeremo ulteriori informazioni di cui hai bisogno.

+0

È il vostro CFC essere chiamato o è l'errore accade prima della chiamata AJAX? –

+1

L'URL IMO fornito deve essere url: "../../WebServices/RMSI/rmsi.cfc?method=UpdateRegularLink" e il metodo deve essere rimosso dai dati. Se l'URL termina con estensione CFC, CFC explorer esegue un kicking e restituisce un codice HTML dei metadati della funzione che potrebbe causare questo problema –

+1

E potrebbe essere che cfcexplorer sta chiedendo l'autorizzazione. puoi verificarlo andando su Strumento per sviluppatori> rete per vedere se è stato richiamato cfcexplorer. –

risposta

4

Così quando si stringificano i dati da passare a coldfusion, coldfusion non lo capisce e aggiunge tutti i tipi di caratteri alla stringa rendendoli illeggibili da coldfusion.

dovuto utilizzare toString() come metodo di chiamata intermediario, poiché il pacchetto JSON si presenta come una matrice di byte (dati binari), che deve essere trasformato in una stringa prima ColdFusion grado di analizzare come valore JSON.

buona anche chiamare @Chandan Kumar per aggiungere il metodo alla fine dell'URL invece di passarlo con i dati. Io in realtà continuato a lanciare su quel pezzo, ma che alla fine è stato come ha funzionato in modo Complimenti a voi

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

         } 
        ); 

CFC

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