So I currently have a setup where when the user clicks a link, the link's ID is sent via AJAX to a coldfusion page that uses it to make a query and return an object.
所以我目前有一个设置,当用户点击链接时,链接的ID通过AJAX发送到coldfusion页面,该页面使用它来进行查询并返回一个对象。
The jquery function looks like this:
jquery函数如下所示:
$(".thelink").click(function(){
var link_id = $(this).attr("id");
var postData ={
link_id: link_id,
}
$.ajax({
type: "post",
url: "includes/query.cfm",
contentType: "application/json",
data:JSON.stringify(postData),
datatype: "json",
success: function(data){
$("#derpaderp").html(data.DATA.NAME);
}
})
})
It sends the data over to the CFM file, which does the query, and generates a single row of data. I then use
它将数据发送到执行查询的CFM文件,并生成单行数据。然后我用
<CFOUTPUT>#SerializeJSON(myQuery, true)#</CFOUTPUT>
to return the data. This all works fine, and I can see this in developer tools:
返回数据。这一切都很好,我可以在开发人员工具中看到这一点:
Now, if you look back at the AJAX function, under "success", I'm trying to basically access the values within the DATA object, and as you can see in the image, I'm getting an error "data.DATA" is undefined.
现在,如果你回顾一下AJAX函数,在“成功”下,我试图基本*问DATA对象中的值,正如你在图像中看到的那样,我收到错误“data.DATA”未定义。
Any ideas how I can get the values of the items in the DATA object (name, visible_url, landing_url, etc...)
任何想法我如何获取DATA对象中的项的值(名称,visible_url,landing_url等...)
Thank you!
Vlad's answer solved the problem above, but a follow up question:
弗拉德的答案解决了上述问题,但后续问题:
If I need to make another query to the CFM file like this:
如果我需要对CFM文件进行另一个查询,如下所示:
<CFOUTPUT>
<cfset object0 = RemoveChars(#SerializeJSON(query1, true)#, 1, 2) >
<cfset object1 = RemoveChars(#SerializeJSON(query2, true)#, 1, 2) >
#object0#
#object1#
</CFOUTPUT>
It gives me a JSON response like this
它给了我这样的JSON响应
I still need to get info from the DATA objects, however now there are two of them, as seen in the photo above
我仍然需要从DATA对象获取信息,但是现在有两个,如上图所示
I'm trying this
我正在尝试这个
$.ajax({
type: "post",
url: "includes/query.cfm",
contentType: "application/json",
data:JSON.stringify(postData),
datatype: "json",
success: function(data){
var pdata = $.parseJSON(data[0]);
console.log(pdata);
}
})
Based on Vlad's answer for the original question, but it's throwing an error.
基于弗拉德对原始问题的回答,但是它引发了一个错误。
2 个解决方案
#1
2
In your success function you can add parseJson function. You can use console.log to check the other values you want.
在您的成功函数中,您可以添加parseJson函数。您可以使用console.log检查所需的其他值。
$.ajax({
type: "post",
url: "includes/query.cfm",
contentType: "application/json",
data:JSON.stringify(postData),
datatype: "json",
success: function(data){
var pdata = $.parseJSON(data);
console.log(pdata.DATA.VISIBLE_URL)
//$("#derpaderp").html(pdata.DATA.VISIBLE_URL);
}
})
#2
0
OK, I figured out the solution for my follow up question.
好的,我找到了我的后续问题的解决方案。
Essentially what was happening was I was sending two separate objects back through the AJAX function. Like this:
基本上发生了什么是我通过AJAX函数发回两个单独的对象。像这样:
{"COLUMNS": x, "ROWCOUNT":Y, "DATA:Z}
{"COLUMNS": x, "ROWCOUNT":Y, "DATA:Z}
This caused a parsing error, as there are 2 objects. I solved this by creating a single object to send back.
这导致解析错误,因为有2个对象。我通过创建一个要发回的对象来解决这个问题。
<cfset object0 = RemoveChars(#SerializeJSON(query1, true)#, 1, 2) >
<cfset object1 = RemoveChars(#SerializeJSON(query2, true)#, 1, 2) >
<CFOUTPUT>
{"FIRST":[#object0#], "SECOND":[#object1#]}
</CFOUTPUT>
And then getting that info in Jquery by doing this:
然后通过执行以下操作在Jquery中获取该信息:
$.ajax({
type: "post",
url: "includes/query.cfm",
contentType: "application/json",
data:JSON.stringify(postData),
datatype: "json",
success: function(data){
var pdata = $.parseJSON(data);
var object1_data = pdata.FIRST[0];
var object2_data = pdata.SECOND[0];
console.log(object1_data.DATA.VISIBLE_URL)
}
})
This successfully logs the value of "VISIBLE_URL" from the first query.
这会成功记录第一个查询中“VISIBLE_URL”的值。
Thanks everyone!
#1
2
In your success function you can add parseJson function. You can use console.log to check the other values you want.
在您的成功函数中,您可以添加parseJson函数。您可以使用console.log检查所需的其他值。
$.ajax({
type: "post",
url: "includes/query.cfm",
contentType: "application/json",
data:JSON.stringify(postData),
datatype: "json",
success: function(data){
var pdata = $.parseJSON(data);
console.log(pdata.DATA.VISIBLE_URL)
//$("#derpaderp").html(pdata.DATA.VISIBLE_URL);
}
})
#2
0
OK, I figured out the solution for my follow up question.
好的,我找到了我的后续问题的解决方案。
Essentially what was happening was I was sending two separate objects back through the AJAX function. Like this:
基本上发生了什么是我通过AJAX函数发回两个单独的对象。像这样:
{"COLUMNS": x, "ROWCOUNT":Y, "DATA:Z}
{"COLUMNS": x, "ROWCOUNT":Y, "DATA:Z}
This caused a parsing error, as there are 2 objects. I solved this by creating a single object to send back.
这导致解析错误,因为有2个对象。我通过创建一个要发回的对象来解决这个问题。
<cfset object0 = RemoveChars(#SerializeJSON(query1, true)#, 1, 2) >
<cfset object1 = RemoveChars(#SerializeJSON(query2, true)#, 1, 2) >
<CFOUTPUT>
{"FIRST":[#object0#], "SECOND":[#object1#]}
</CFOUTPUT>
And then getting that info in Jquery by doing this:
然后通过执行以下操作在Jquery中获取该信息:
$.ajax({
type: "post",
url: "includes/query.cfm",
contentType: "application/json",
data:JSON.stringify(postData),
datatype: "json",
success: function(data){
var pdata = $.parseJSON(data);
var object1_data = pdata.FIRST[0];
var object2_data = pdata.SECOND[0];
console.log(object1_data.DATA.VISIBLE_URL)
}
})
This successfully logs the value of "VISIBLE_URL" from the first query.
这会成功记录第一个查询中“VISIBLE_URL”的值。
Thanks everyone!