8

フォームデータを収集してjqueryオブジェクトに入れるjquery submitイベントがあります。その jquery オブジェクトを取得して、coldfusion Web サービスに渡し、そこでそれを使用して xml ファイルを更新したいと考えています。Web サービスからの応答は必要ありません。応答を Web サービスに送信し、そこからのデータをいじりたいだけです。

クライアント側/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>

Chrome では「Unauthorized」と表示され、firebug では「unexpected character」と表示されます

私に聞いてください。必要な情報を追加します。

4

1 に答える 1

6

そのため、coldfusion に渡すデータを文字列化すると、coldfusion はそれを理解せず、あらゆる種類の文字を文字列に追加して、coldfusion で読み取れないようにします。

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

また、@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 );

                        }
                    );

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>
于 2013-10-03T14:17:39.987 に答える