通过AJAX Post将数据传递给CFC使用JSON

时间:2021-10-20 13:17:24

I have a jquery submit event that collects form data and puts it into a jquery object. I want to take that jquery object and pass it to a coldfusion web service where I can use it to update an xml file. I do not want a response from the web service I just want to send it to the web service and fiddle with the data from there.

我有一个jquery提交事件,它收集表单数据并将其放入jquery对象。我想获取该jquery对象并将其传递给coldfusion Web服务,我可以使用它来更新xml文件。我不希望Web服务的响应,我只想将其发送到Web服务并从那里调整数据。

Client Side/JQuery:

客户端/ 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:

服务器端/ 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 I get "Unauthorized" In firebug I get "unexpected character"

在Chrome中我得到“未经授权”在萤火虫中我得到“意外的性格”

Just ask me and I will add more information that you need.

只要问我,我会添加您需要的更多信息。

1 个解决方案

#1


4  

So when you stringify the data to be passed to coldfusion, coldfusion doesn't understand it and adds all kinds of characters to your string making it unreadable by coldfusion.

因此,当您将要传递给coldfusion的数据进行字符串化时,coldfusion不会理解它并将各种字符添加到您的字符串中,这使得coldfusion无法读取。

Had to use toString() as an intermediary method call since the JSON packet comes across as a byte array (binary data) which needs to be turned back into a string before ColdFusion can parse it as a JSON value.

不得不使用toString()作为中间方法调用,因为JSON数据包作为字节数组(二进制数据)出现,需要在ColdFusion将其解析为JSON值之前将其转换回字符串。

also good call @Chandan Kumar for adding the method to the end of the url instead of passing it in with the data. I actually kept flipping on that piece but that ultimately was how it worked so KUDOS to you

也很好地调用@Chandan Kumar将方法添加到url的末尾,而不是将其传递给数据。我实际上一直在翻看这件作品,但最终它是如何起作用的,所以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 );

                        }
                    );

THE CFC

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>

#1


4  

So when you stringify the data to be passed to coldfusion, coldfusion doesn't understand it and adds all kinds of characters to your string making it unreadable by coldfusion.

因此,当您将要传递给coldfusion的数据进行字符串化时,coldfusion不会理解它并将各种字符添加到您的字符串中,这使得coldfusion无法读取。

Had to use toString() as an intermediary method call since the JSON packet comes across as a byte array (binary data) which needs to be turned back into a string before ColdFusion can parse it as a JSON value.

不得不使用toString()作为中间方法调用,因为JSON数据包作为字节数组(二进制数据)出现,需要在ColdFusion将其解析为JSON值之前将其转换回字符串。

also good call @Chandan Kumar for adding the method to the end of the url instead of passing it in with the data. I actually kept flipping on that piece but that ultimately was how it worked so KUDOS to you

也很好地调用@Chandan Kumar将方法添加到url的末尾,而不是将其传递给数据。我实际上一直在翻看这件作品,但最终它是如何起作用的,所以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 );

                        }
                    );

THE CFC

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>