嗨,这是我在stackoverflow上的第一个问题:) 试图让coldfusion使用javascript。
我正在尝试从数据库中提取数据并在我的页面中显示它。 但我意识到js是一种客户端语言,并且是服务器端。 我不能只在函数内运行cfquery所以我有两个文件
一个是我的index.html
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
</head>
<body class="jumbotron">
<div class="container text-center " >
<div class="row">
<img class="col-sm-4 col-sm-offset-4" src="emp.jpg"/>
</div>
<div class="row" style="margin-top:20px" >
<div class="col-sm-6 col-sm-offset-3">
<div id="imaginary_container">
<div class="input-group stylish-input-group">
<input type="text" class="form-control" id="searchBar" name="searchBar" placeholder="Search" >
<span class="input-group-addon">
<button onclick="search(global,$http)">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
</div>
</div>
</div>
</div>
<script>
function search($scope, $http) {
var searchQ = document.getElementById('searchBar').value;
$http.get('/grid.cfc?method=getContact&returnformat=json').
success(function (response) {
$scope.todos = data.DATA;
}).
error(function (data) {
$scope.todos = data;
});
};
</script>
</body>
</html>
一个是我的grid.cfc
<cfcomponent>
<cffunction name="getContact" access="remote" >
<cfargument name="firstName" default="">
<cfargument name="lastName" default="">
<cfquery name="searchQry" datasource="MehrabanDSource">
SELECT
*
FROM Contacts
WHERE FirstName=fname;
</cfquery>
<cfreturn searchQry>
</cffunction>
</cfcomponent>
你能帮助我理解我的代码吗?我真的不知道我是否在正确的轨道上或如何将变量传递给我的grid.cfm所以它知道要查找的名字。感谢很多提前:)
答案 0 :(得分:0)
<cfajaxproxy cfc="functions" jsclassname="ajax_proxy" />
<cfajaximport>
<script>
function send_ajax(searchBar) {
var instance = new ajax_proxy();
instance.setCallbackHandler(return_ajax);
instance.functionality(searchBar);
}
function return_ajax(result) {
document.getElementById('output').innerHTML = result;
}
</script>