简体   繁体   English

ColdFusion AJAX:在参数中未定义元素

[英]ColdFusion AJAX: Element is undefined in arguments

I'm doing some AJAX ColdFusion testing and I have an issue while sending data to ColdFusion server using AJAX. 我正在做一些AJAX ColdFusion测试,使用AJAX将数据发送到ColdFusion服务器时遇到问题。 Here is the HTML code I have: 这是我拥有的HTML代码:

<form method="post">
    <p>
        <input type="text" id="user_name" name="name" placeholder="name" value="test" />
        <input type="text" id="user_email" name="email" placeholder="email" value="abc" />
    </p>
    <input type="button" value="Find Out" class="ajax_form"/>
</form>

<p id="insert"></p>

JS: JS:

$(document).ready(function (){
    $(".ajax_form").click(function (){
        var that = $(this).parent(),
            url = "ajax.cfc",
            type = that.attr('method'),
            test = {};

        that.find('[name]').each(function(index, value){
            var that = $(this),
                name = that.attr('name'),
                value = that.val();

            test[name] = value;
        });

        console.log(test.name);

        $.ajax({
            url: url,
            type: type,
            data: {
                method: "testCheck",
                name: test.name,
                email: test.email,
                array: test
            },

            success: function (responce){
                document.getElementById('insert').innerHTML = responce;
            },
            error: function (xhr, textStatus, errorThrown){
                alert(errorThrown);
            }
        });
    });
});

ajax.cfm is really simple and looks like this. ajax.cfm非常简单,看起来像这样。

<cfcomponent output="false">
    <cffunction name="testCheck" access="remote" returnformat="plain" returnType="string" output="false">
        <cfargument name="name" type="string" required="false" />
        <cfargument name="email" type="string" required="false" />
        <cfargument name="array" required="false" />

        <cfset var string = " " />

        <cfif (arguments.name) eq "test">
            <cfset string = arguments.array/>
        <cfelse>
            <cfset string = "incorrect" />
        </cfif>

        <cfreturn string />
    </cffunction>
</cfcomponent>

The problem is that cf argument isn't defined (the error I get: "500 (Element ARRAY is undefined in ARGUMENTS)") or gets default value when I set it. 问题是未定义cf参数(我得到的错误是:“ 500(元素数组在ARGUMENTS中未定义)”)或在设置它时获取默认值。 I have a guess that this is about data type of the object I'm sending, but I can't come up with any solution. 我猜测这与我要发送的对象的数据类型有关,但是我无法提出任何解决方案。 Is it possible at all to use this type of data as cfargument is this case? 这种情况下完全有可能使用这种类型的数据吗?

The error is mainly due to following reasons: 该错误主要是由于以下原因:

  1. array as an object(JSON): array作为对象(JSON):

You are sending array as an object so the request will be made with a contentType of application/x-www-form-urlencoded by jQuery (as you have not explicitly specified it). 您正在将array作为对象发送,因此请求将使用由jQuery application/x-www-form-urlencodedapplication/x-www-form-urlencodedcontentType发出(因为您没有明确指定它)。

Hence, the object( array in the case) will be serialized into name/value pairs and will be appended in the URL so, the server will get following arguments(which was not desired): 因此,对象(在这种情况下为array )将被序列化为名称/值对,并将被附加在URL中,因此服务器将获得以下参数(不需要):

array[name] = 'test'

array[email] = 'abc'

So, you have to serialize the data from client side and then deserialize at the server side like this: 因此,您必须从客户端序列化数据,然后在服务器端进行反序列化,如下所示:

   $.ajax({
        url: url,
        type: type,
        data: {
            method: "testCheck",
            name: test.name,
            email: test.email,
            array: JSON.stringify(test) <--- Serialize here -->
        }

        , success: function (responce){
            document.getElementById('insert').innerHTML = responce;
        }
        , error: function (xhr, textStatus, errorThrown){
            alert(errorThrown);
        }
    });

<--- Deserialize --->
<cfset local.array = deserializeJSON(arguments.array)>
  1. argument array has no default 参数数组没有默认值

The argument array has no default value and was not passed during the request(due to the above mentioned reason) so, it would be undefined . 参数array没有默认值,并且在请求期间未传递(由于上述原因),因此它将是undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM