我是Jquery和javascript的新手,我使用ajax上传我的图片并希望获取图片网址,我尝试了它,我一直都在收到错误。有人可以帮我解决这个问题吗?谢谢!
我收到错误:
Uncaught TypeError: Cannot read property 'length' of undefined
以下是调用url后的结果:
{"status_code":200,"status_txt":"OK","data":{"img_name":"nfFWV.jpg","img_url":"http:\/\/sl.uploads.im\/nfFWV.jpg","img_view":"http:\/\/uploads.im\/nfFWV.jpg","img_width":"3840","img_height":"2160","img_attr":"width=\"3840\" height=\"2160\"","img_size":"3.1 MB","img_bytes":3226508,"thumb_url":"http:\/\/sl.uploads.im\/t\/nfFWV.jpg","thumb_width":360,"thumb_height":203,"source":"base64 image string","resized":"0","delete_key":"338aa524f1654056"}}
这是我的代码:
var imgfile = $('#receipt').get(0).files[0];
$.ajax({
type: 'POST',
url: 'http://uploads.im/api?upload='+imgfile,
dataType: 'json',
data:formData,
cache: false,
contentType: false,
processData: false,
success: function(data){
var htmlStr = '';
$.each(data, function(k, v){
$.each(v.data, function(s, a){
htmlStr += a.img_url + '<br />';
});
});
alert(htmlStr);
}
});
答案 0 :(得分:0)
答案 1 :(得分:0)
如果你想访问返回的json的个别属性,那么你可以这样做。
var imgObj = data.data // your response in success callback
var imgName = imgObj.img_name;
var imgUrl= imgObj.img_url;
// in this way you can access rest of property
不需要任何类型的循环,例如$.each