Post parameters from AJAX request undefined in form scope in ColdFusion

后端 未结 2 1268
没有蜡笔的小新
没有蜡笔的小新 2021-01-22 17:54

I am working on a ColdFusion 8 training application where I\'m making some AJAX requests (without any libraries such as jQuery) to support a very basic CRUD application. The h

相关标签:
2条回答
  • 2021-01-22 18:39

    There absolutely is a FORM scope when you do an Ajax post to a CFC.

    This example POSTs form data via Ajax to a CFC function with no arguments and returns the JSON format of the FORM scope. Yes, you should have arguments to document, specify required/not required and data type, but they're not mandatory.

    Is there any reason you aren't using jQuery? It would probably make your life much easier.

    There must be something wrong with how you're sending the form data to the Ajax call. If you use FireBug to watch your Ajax calls, you can see the POSTed parameters.

    HTML

    <html>
        <head>
            <title>Ajax POST to CFC</title>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
            <script type="text/javascript" src="test.js">
        </head>
        <body>
    
            <form id="foo" action="" method="post">
    
                <input type="text" id="a" name="a" value="Hello" />
                <br />
                <input type="text" id="b" name="b" value="Goodbye" />
                <br />
    
                <textarea id="data" cols="30" rows="10" disabled="true"></textarea>
                <br />
                <input type="button" id="btnSubmit" value="Do Ajax!" />
    
            </form>
    
        </body>
    
    </html>

    JavaScript

    $(document).ready(function() {
        $('#btnSubmit').on('click', function() {
            $.ajax({
                asynch : true,
                type : 'POST',
                dataType : 'json',
                url : 'test.cfc?method=testing&returnformat=json',
                data : {
                    a : $('#a').val(),
                    b : $('#b').val()
                },
                success : function(data, textStatus) {
                    $('#data').val(JSON.stringify(data));
                }
            });
        });
    });

    CFC

    <cfcomponent>
        <cffunction name="testing" access="remote" output="false" returntype="string">
            <cfreturn serializeJSON( form ) />
        </cffunction>> 
    </cfcomponent>


    Old School, no jQuery, just plain ol' JavaScript

    I found a simple example of an Ajax POST without jQuery here: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

    HTML
    Remove the jQuery SCRIPT tag, change the other SCRIPT to test-nojq.js and change the submit button to add an onclick event.

    <input type="button" id="btnSubmit" value="Do Ajax!" onclick="doSubmit();" />

    JavaScript: test-nojq.js

    function doSubmit(){
        var http = new XMLHttpRequest();
        var url = "test.cfc";
        var params = "method=testing&returnformat=json";
            params += "&a=" + document.getElementById('a').value;
            params += "&b=" + document.getElementById('b').value;
        http.open("POST", url, true);
        //Send the proper header information along with the request
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.setRequestHeader("Content-length", params.length);
        http.setRequestHeader("Connection", "close");
        http.onreadystatechange = function() {//Call a function when the state changes.
            if(http.readyState == 4 && http.status == 200) {
                document.getElementById('data').value = http.responseText;
            }
        }
        http.send(params);
    }

    0 讨论(0)
  • 2021-01-22 18:54

    Make newLoc into an argument and it should work.

    <cffunction name="addNewLocation" output="false" access="remote">
      <cfargument name="newLoc">
      ...
    
    </cffunction>
    

    update: not sure why I encountered no form scope one time calling a remote method. Anyway, it isn't true but the rest of the answer should still hold true.

    0 讨论(0)
提交回复
热议问题