ColdFusion AJAX:在参数中未定义元素 [英] ColdFusion AJAX: Element is undefined in arguments
问题描述
我正在做一些AJAX ColdFusion测试,使用AJAX将数据发送到ColdFusion服务器时遇到问题. 这是我拥有的HTML代码:
I'm doing some AJAX ColdFusion testing and I have an issue while sending data to ColdFusion server using AJAX. Here is the HTML code I have:
<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:
$(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非常简单,看起来像这样.
ajax.cfm is really simple and looks like this.
<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>
问题是未定义cf参数(我得到的错误是:"500(ARGUMENTS中未定义元素数组)")或在设置它时获取默认值.我猜想这与我要发送的对象的数据类型有关,但是我无法提出任何解决方案.这种情况下完全有可能使用这种类型的数据吗?
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. 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:
-
array
作为对象(JSON):
array
as an object(JSON):
您将array
作为对象发送,因此jQuery将使用application/x-www-form-urlencoded
的contentType
发出请求(因为您尚未明确指定).
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
)将被序列化为名称/值对,并将被附加在URL中,因此服务器将获得以下参数(不需要):
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[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)>
- 参数数组没有默认值
参数array
没有默认值,并且在请求期间未传递(由于上述原因),因此它将是undefined
.
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
.
这篇关于ColdFusion AJAX:在参数中未定义元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!