Reputation: 93
I have a pretty simple Ajax-Request. It just requests some data from a database. The problem is that I'm always getting an JSON.parse error (first row, first column). The JSON the Coldfusion function outputs is valid and correct.
When I just copy the code to a .cfc file everything works just fine. Why is that?
Coldfusion:
<cffunction name="getAllDatatypes" access="remote" returntype="query" returnformat="json">
<cfquery name="getAllDatatypes" datasource="#DSN#">
SELECT
id_datatype,
name
FROM
datatype
</cfquery>
<cfreturn getAllDatatypes>
</cffunction>
JQuery:
function getAllDatatypes() {
$.ajax({
url: "getData.cfm",
type: "post",
dataType: "json",
data: {method: "getAllDatatypes"}
}).done(function(result) {
console.log(result);
}).fail(function(error1, error2, error3) {
console.log(error1 + " " + error2 + " " + error3);
});
}
Upvotes: 2
Views: 205
Reputation: 15846
When you create a remote
function in a .cfc
file, the CFC path starts to act like a webservice would. When you make the call like following,
http://localhost/components/testcomp.cfc?method=getAllDatatypes
ColdFusion knows that you are trying to invoke a remote function getAllDatatypes
in the components/testcomp.cfc
component and it is invoked if remote function getAllDatatypes
is found and the response is returned as either plaintext
, xml
or json
.
On the other hand. Any calls to a .cfm
file, there is not such scenario. Even if you have created a function in the file, ColdFusion is not looking to a remote method call into a .cfm
file.
PS: It is always a good idea to initialize variables in side a function using var
or use local
scope.
Upvotes: 4