Javascript 如何读取AJAX响应变量?

Javascript 如何读取AJAX响应变量?,javascript,jquery,ajax,Javascript,Jquery,Ajax,以下是我在AJAX成功函数中收到的响应: "{"success":true,"data":{"id":1841,"title":"atitle","filename":"filename.jpg","url":"http:\/\/example.com\/wp-content\/uploads\/2014\/11\/filename.jpg","link":"http:\/\/example.com\/?attachment_id=1841","alt":"","author":"21","de

以下是我在AJAX成功函数中收到的响应:

"{"success":true,"data":{"id":1841,"title":"atitle","filename":"filename.jpg","url":"http:\/\/example.com\/wp-content\/uploads\/2014\/11\/filename.jpg","link":"http:\/\/example.com\/?attachment_id=1841","alt":"","author":"21","description":"","caption":"","name":"filename-39","status":"inherit","uploadedTo":0,"date":1415555051000,"modified":1415555051000,"menuOrder":0,"mime":"image\/jpeg","type":"image","subtype":"jpeg","icon":"http:\/\/example.com\/wp-includes\/images\/media\/file.png","dateFormatted":"November 9, 2014","nonces":{"update":"b832c2939d5","delete":"83dda46357e","edit":"51ac41b11c6"},"editLink":"http:\/\/example.com\/wp-admin\/post.php?post=1841&action=edit","meta":false,"authorName":"Some One","filesizeInBytes":10755,"filesizeHumanReadable":"11 kB","sizes":{"thumbnail":{"height":90,"width":90,"url":"http:\/\/example.com\/wp-content\/uploads\/2014\/11\/filename-90x90.jpg","orientation":"landscape"},"full":{"url":"http:\/\/example.com\/wp-content\/uploads\/2014\/11\/filename.jpg","height":260,"width":236,"orientation":"portrait"}},"height":260,"width":236,"orientation":"portrait","compat":{"item":"","meta":""}}}"
我试图使用此响应中返回的数据更新页面上特定图像的
src
属性。例如:

$( '#myimage' ).attr( 'src', response.data.url );
$.getJSON('remote_url', function(response) {
    if(response.success) {
        $('#myimage').attr('src', response.data.url);
    }
});
问题是,我得到了错误
uncaughttypeerror:无法读取未定义的属性“url”


我确信
response.data.url
是错误的。如何从响应中获取URL,以便更新图像的
src
属性

使用JSON.parse作为对象进行解析,返回的数据是字符串:

response=JSON.parse(response);
$( '#myimage' ).attr( 'src', response.data.url );

使用JSON.parse作为对象进行解析,返回的数据为字符串:

response=JSON.parse(response);
$( '#myimage' ).attr( 'src', response.data.url );
你可以用

x=$.parseJSON(response)
这将把json字符串转换为有效的json对象,如果出现错误,将抛出异常,您可以使用try{}catch(e){}来修复它

try{
var x=$.parseJSON(response);
}catch(e){
console.log(e);
}
你可以用

x=$.parseJSON(response)
这将把json字符串转换为有效的json对象,如果出现错误,将抛出异常,您可以使用try{}catch(e){}来修复它

try{
var x=$.parseJSON(response);
}catch(e){
console.log(e);
}

您可能能够利用jQuery的方法。当您使用ajax时,您只会收到一个字符串响应,因此首先必须使用json将其解析为json。但是,getJSON将为您执行parseJSON

$.getJSON('my/service', function(data) {
    $('#myimage').attr('src', data.url);
});

您可能能够利用jQuery的方法。当您使用ajax时,您只会收到一个字符串响应,因此首先必须使用json将其解析为json。但是,getJSON将为您执行parseJSON

$.getJSON('my/service', function(data) {
    $('#myimage').attr('src', data.url);
});

简单的方法是使用如下所示的
AJAX
请求:

$.post('remote_url', {key:value}, function(response){
    if(response.success) {
        $('#myimage').attr('src', response.data.url);
    }
}, 'json');
在本例中,我使用了
$.post
,但您没有提供足够的关于请求类型的信息,因此它可能也是
$.getJSON
请求。另外,
{key:value}
是一个对象,如果需要,它将被传递给服务器。所以,若您将任何数据传递给服务器,请使用它,否则请删除它

在本例中,
'json'
被用作数据类型,因此响应将由
jQuery
解析。如果您使用
$。getJSON
则将解析响应,但在本例中,您不需要使用
'json'
作为最后一个参数。例如:

$( '#myimage' ).attr( 'src', response.data.url );
$.getJSON('remote_url', function(response) {
    if(response.success) {
        $('#myimage').attr('src', response.data.url);
    }
});

注意:
getJSON
使用
GET
HTTP请求从服务器加载JSON编码的数据。因此,对于
POST
请求,请使用
$.POST

简单的方法是使用
AJAX
请求,如下所示:

$.post('remote_url', {key:value}, function(response){
    if(response.success) {
        $('#myimage').attr('src', response.data.url);
    }
}, 'json');
在本例中,我使用了
$.post
,但您没有提供足够的关于请求类型的信息,因此它可能也是
$.getJSON
请求。另外,
{key:value}
是一个对象,如果需要,它将被传递给服务器。所以,若您将任何数据传递给服务器,请使用它,否则请删除它

在本例中,
'json'
被用作数据类型,因此响应将由
jQuery
解析。如果您使用
$。getJSON
则将解析响应,但在本例中,您不需要使用
'json'
作为最后一个参数。例如:

$( '#myimage' ).attr( 'src', response.data.url );
$.getJSON('remote_url', function(response) {
    if(response.success) {
        $('#myimage').attr('src', response.data.url);
    }
});

注意:
getJSON
使用
GET
HTTP请求从服务器加载JSON编码的数据。因此,对于
POST
请求,请使用
$.POST

我们可以看到完整的
$.ajax
代码吗?有几件事我想知道:a)您是否试图在成功回调中使用response.data.url,以及b)返回数据是否被视为字符串?我们可以看到完整的
$.ajax
代码吗?有几件事我很好奇:a)您是否试图在成功回调中使用response.data.url,以及b)返回数据是否被视为字符串?他使用jQuery的所有ie浏览器都不支持JSON.parse。我认为最好使用jQuery函数(它是跨浏览器的)JSON.parse()不是这样工作的。信息技术ie:
var newvalue=JSON.parse(stringvalue)
@Ernesto哪些浏览器不支持JSON.parse?@henrywright Internet explorer 8及以下版本,他正在使用jQuery,因此最好使用IE8和IE支持的$.parseJSONIE7@Ernesto
$.parseJSON
完美地完成了这项工作。Thank youJSON.parse不是所有ie浏览器都支持的,他正在使用jQuery。我认为最好使用jQuery函数(它是跨浏览器的)JSON.parse()不是这样工作的。信息技术ie:
var newvalue=JSON.parse(stringvalue)
@Ernesto哪些浏览器不支持JSON.parse?@henrywright Internet explorer 8及以下版本,他正在使用jQuery,因此最好使用IE8和IE支持的$.parseJSONIE7@Ernesto
$.parseJSON
完美地完成了这项工作。谢谢你的详细回答。我决定使用一个简单的
response=$.parseJSON(response.response)
,然后让我做
response.data.url
谢谢你的详细回答。我决定使用一个简单的
response=$.parseJSON(response.response)
,然后让我做
response.data.url
谢谢你的帮助-我决定使用
$.parseJSON()
谢谢你的帮助-我决定使用
$.parseJSON()