jemz
February 14, 2014, 1:06pm
1
Hi, I need your help please…I don’t know how to acces this in jquery i want to get the id which is 001,but i get an error “JSON.parse: unexpected non-whitespace character after JSON data”
This is the return in my firebug {“id”:“001”}
$.ajax({
type: 'post',
data: {officeid:id},
url: 'toOtherpage.cfm',
success:function(data){
var dtaobj = jQuery.parseJSON(data);
console.log(dtaobj.id);
}
Thank you in advance.
Hi jemz,
Your syntax is correct.
Can you log data to the console and post the output here?
$.ajax({
type: 'post',
data: {officeid:id},
url: 'toOtherpage.cfm',
success:function(data){
console.log(data);
}
});
jemz
February 14, 2014, 1:51pm
3
James_Hibbard:
Hi jemz,
Your syntax is correct.
Can you log data to the console and post the output here?
$.ajax({
type: 'post',
data: {officeid:id},
url: 'toOtherpage.cfm',
success:function(data){
console.log(data);
}
});
Hi Pullo,
$.ajax({
type: 'post',
data: {officeid:id},
url: 'toOtherpage.cfm',
success:function(data){
console.log("data=",data);
}
});
[/QUOTE]
This is the output
data={“id”:“001”}
Sorry, I should have seen it before.
$.parseJSON takes a JSON string and returns the resulting JavaScript object.
This means that you need to do this:
var dtaobj = $.parseJSON('{"id":"001"}');
console.log(dtaobj.id);
=> 001
or better still:
$.ajax({
type: 'post',
data: {officeid:id},
url: 'toOtherpage.cfm',
success:function(data){
console.log(data.id);
}
});
How are you returning the data from your cfm file?
jemz
February 14, 2014, 3:32pm
7
This is in my
toOtherpage.cfm
<cfif IsDefined("officeid")>
<cfset mydata = officeId(#officeid#)>
<cfoutput>#mydata#</cfoutput>
</cfif>
<cffunction name="officeId">
<cfargument name="officeid">
<cfquery name="officequery" datasource="#mydatasource#">
Select officeid from office_tbl where officeid = '#officeid#'
</cfquery>
<cfset theStruct = StructNew()>
<cfset theStruct.officeid=#officeQuery.officeid#>
<cfreturn SerializeJSON(theStruct)>
</cffunction>
So you seem to be returning JSON with the call to SerializeJSON
If you add this to your success callback, what is output?
console.log(typeof data);
Try setting the data type to JSON
$.ajax({
type: 'post',
data: {officeid:id},
dataType: 'json',
url: 'toOtherpage.cfm',
success:function(data){
console.log(typeof data);
}
});
What happens then?
jemz
February 14, 2014, 4:05pm
11
still it will print string
jemz
February 14, 2014, 4:49pm
12
I don’t understand why i could not get the office id,but the response return in json format
According to the docs , serializeJSON
returns a string that contains a JSON representation of the parameter value.
This makes sense, as typeof(data)
returns “String”, but unfortunately, for whatever reason, $.parseJSON
is choking and dying.
I guess that there’s something about the string that it cannot handle, as this:
var dtaobj = $.parseJSON('{"id":"001"}');
console.log(dtaobj.id);
=> 001
works as expected.
Anyway, next idea, could you not have ColdFusion serialize the query object to JSON?
<cffunction name="officeId" access="remote" returnType="query" returnFormat="JSON">
<cfargument name="officeid">
<cfquery name="officequery" datasource="#mydatasource#">
Select officeid from office_tbl where officeid = '#officeid#'
</cfquery>
<cfset theStruct = StructNew()>
<cfset theStruct.officeid=#officeQuery.officeid#>
<cfreturn SerializeJSON(theStruct)>
</cffunction>
I don’t know much about ColdFusion, so no idea if this’ll work.
You’ll need to keep the dataType: “json” in your $.ajax() call:
$.ajax({
type: 'post',
data: {officeid:id},
dataType: 'json',
url: 'toOtherpage.cfm',
success:function(data){
console.log(typeof data);
}
});
jemz
February 15, 2014, 5:31am
14
HI pullo,
I fixed it now…there is an element including in the response…i did not notice it.
Thank you.
That’s good.
Just for my own curiosity, what did you change?