Javascript jQuery获取无法作为对象访问的数据
我从jQuery get请求中得到这个结果,但它不被视为对象Javascript jQuery获取无法作为对象访问的数据,javascript,jquery,object,get,Javascript,Jquery,Object,Get,我从jQuery get请求中得到这个结果,但它不被视为对象 [{ "amount": "19323", "image_tag_id": "1", "language_iso": "en", "image_tag": "bla", "image_content_id": "1", "image_id": "1", "last_change": "2010-08-18 09:46:53", "description": "bla pr
[{
"amount": "19323",
"image_tag_id": "1",
"language_iso": "en",
"image_tag": "bla",
"image_content_id": "1",
"image_id": "1",
"last_change": "2010-08-18 09:46:53",
"description": "bla presented by a hairy fly",
"title": "bla_presented_by_a_hairy_fly",
"alt_text": "bla presented by a hairy fly"
}]
这就是我得到的结果。
我需要在页面上显示金额。
但现在,如果我将其作为“imagecount”结果,并询问imagecount.amount
或imagecount[0]。amount
未定义
echo json_encode($results); //gives the results
$.get('/file.php', imageSearchData, function(imagecount) {
$('.imageAmount').html(imagecount[0].amount);
});
调用该文件。由于您在服务器端对数据进行编码,因此在使用前应在客户端对其进行解析,因此您可以使用或进行解析:
$.get('/file.php', imageSearchData, function(imagecount) {
imagecount = JSON.parse(imagecount);
//OR
//imagecount = $.parseJson(imagecount);
$('.imageAmount').html(imagecount[0].amount);
});
希望这能有所帮助。由于您在服务器端对数据进行编码,所以在使用之前应该在客户端对其进行解析,因此您可以使用或进行解析:
$.get('/file.php', imageSearchData, function(imagecount) {
imagecount = JSON.parse(imagecount);
//OR
//imagecount = $.parseJson(imagecount);
$('.imageAmount').html(imagecount[0].amount);
});
希望这有帮助。使用JSON.parse()
来解决这个问题
$.get('/file.php', imageSearchData, function(imagecount) {
var imagecountParsed = JSON.parse(imagecount);
$('.imageAmount').html(imagecountParsed[0].amount);
});
使用JSON.parse()
解决此问题
$.get('/file.php', imageSearchData, function(imagecount) {
var imagecountParsed = JSON.parse(imagecount);
$('.imageAmount').html(imagecountParsed[0].amount);
});
普通的
$.get
,获取一个json字符串。尝试在已解析json对象的位置使用$.getJSON
$.getJSON('/file.php', imageSearchData, function(imagecount) {
$('.imageAmount').html(imagecount[0].amount);
});
普通的
$.get
,获取一个json字符串。尝试在已解析json对象的位置使用$.getJSON
$.getJSON('/file.php', imageSearchData, function(imagecount) {
$('.imageAmount').html(imagecount[0].amount);
});
您可能将JSON作为字符串获取。如果是这种情况,那么您需要对JSON字符串运行
JSON.parse
。尝试JSON.parse(imagecount[0].amount)使用$.getJSON
自动反序列化字符串,或者确保在PHP的头中设置正确的应用程序/JSON
响应类型。您是否在PHP代码中手动构建字符串(即使用连接)?如果是这样的话,你真的应该改成使用json\u encode()!现在我的php中有一个sendContentType('application/json')。所以我只需要做一次,然后用它进行更多的查询。如果是这种情况,那么您需要对JSON字符串运行JSON.parse
。尝试JSON.parse(imagecount[0].amount)使用$.getJSON
自动反序列化字符串,或者确保在PHP的头中设置正确的应用程序/JSON
响应类型。您是否在PHP代码中手动构建字符串(即使用连接)?如果是这样的话,你真的应该改成使用json\u encode()!现在我的php中有一个sendContentType('application/json')。所以我只需要做一次,然后用它进行更多的查询。