gpt4 book ai didi

jQuery getJSON 可排序回调

转载 作者:行者123 更新时间:2023-12-01 05:58:19 26 4
gpt4 key购买 nike

我正在学习一个实现可排序列表的教程,但我无法判断 json 是否正在加载。有没有办法使用 Firebug 和 Firequery 来查看它是否被调用或者是否是一个错误的地址?我没有看到任何错误。我的 html 和 Coldfusion :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org  /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<link rel="stylesheet" href="../css/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>



<script type="text/javascript">
$(function() {
$sortable = $("#sortable").sortable({
update: function(event, ui){
updateOrder();
}
}).disableSelection();


function updateOrder(){
$.getJSON(
'remote.cfc?wsdl',
{
method : 'updateOrder',
key: '<cfoutput>#session.ajaxkey#</cfoutput>',
orderedList: $sortable.sortable('toArray').toString(),
returnformat: 'json',
queryformat: 'column',
},
callback
);
}
});

function callback(json){
if ( json.RESULT )
{
$('#serverresult').text( "Order updated to: " + json.NEWORDER );
}
else
{
$('#serverresult').text( "Something went wrong!" );
}
}
</script>
</head>

<body>

<body>
<h1>Menu List</h1>

<cfquery name="rsSort" datasource="#DataSource#">
SELECT * FROM `test-sort` ORDER by Sort
</cfquery>

<div class="demo">
<ul id="sortable">
<cfoutput>
<cfloop query="rsSort">
<li id="id_#ID#">#Title# #ID#</li>

</cfloop>
</cfoutput>
</ul>
<div id="serverresult">
</div>
</div>



</body>
</html>

被调用的文件:

<cfcomponent output="false" hint="I respond to AJAX requests">


<cffunction name="updateOrder" output="false" access="remote">
<cfargument name="orderedList" required="true" type="string">
<cfargument name="key" required="true" type="string">

<cfset var ndx = "">
<cfset var id = 0>
<cfset var position = 0>
<cfset var updatedids = "">
<cfset var result = {result="false"}>

<!--- do a basic security check --->
<cfif isAllowed( arguments.key )>

<!--- Prevent race conditions --->
<cflock name="updateOrder" timeout="60">
<cftransaction>
<cfloop list="#arguments.orderedList#" index="ndx">

<cfset id = Val( ListLast( ndx, "_" ) )>
<cfset position = position+1>
<cfset updatedids = ListAppend( updatedids, id )>

<cfquery datasource="#DataSource#">
update `test-sort` set
Sort = <cfqueryparam value="#position#" cfsqltype="cf_sql_integer">
where
id = <cfqueryparam value="#id#" cfsqltype="cf_sql_integer">
</cfquery>

</cfloop>

<!--- delete any items not in the list --->
<cfquery datasource="#DataSource#">
delete from `test-sort`
where id not in ( <cfqueryparam value="#updatedids#" cfsqltype="cf_sql_integer" list="true"> )
</cfquery>

</cftransaction>
</cflock>

<cfset result = {result="true", neworder=updatedids, position=position }>

</cfif>

<cfreturn result>
</cffunction>


<cffunction name="isAllowed" output="false" access="private" returntype="boolean">
<cfargument name="key" required="true">
<cfset var result = false>
<!--- check that request is coming from the same browser that created the session --->
<cfif IsDefined( "session.ajaxkey" ) AND ( session.ajaxkey eq arguments.key )>
<cfset result = true>
</cfif>
<cfreturn result>
</cffunction>

</cfcomponent>

最佳答案

您几乎肯定不希望在您要使用 ajax 的 URL 中包含 ?wsdl。这只会返回 CFC 的 WSDL XML。希望 Firebug 能证明这一点。您还可以使用Fiddler调试网络流量(适用于所有浏览器)

您可能还需要在 cffunction 中设置 returnformat="JSON",以便 CF 将您返回的结构转换为 JSON。

关于jQuery getJSON 可排序回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13327779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com