开发者

javascript access xmlhttprequest

I am returning xmlhttprequest and am trying to access xmlhttprequest.responseText

I can use console.log and see the value of xmlhttprequest.state, but when I try to see the value of xmlhttprequest.responseText nothing comes up. Here is the responseText I get when I log the xmlhttprequest object.

{"name":"05571C-83E2A2-D4F3F0-6D7EE3-58A5F6-606843_2009-1.jpg",
"size":54823,
"type":"application\/o开发者_StackOverflowctet-stream",
"url":"\/admin\/..\/uploads\/05571C-83E2A2-D4F3F0-6D7EE3-58A5F6-606843_2009-1.jpg",
"large_url":"\/admin\/..\/uploads\/large\/05571C-83E2A2-D4F3F0-6D7EE3-58A5F6-606843_2009-1.jpg",
"medium_url":"\/admin\/..\/uploads\/medium\/05571C-83E2A2D4F3F0-6D7EE3-58A5F6-606843-2009-1.jpg",
"thumbnail_url":"\/admin\/..\/uploads\/thumbs\/05571C-83E2A2-D4F3F0-6D7EE3-58A5F6-606843-2009-1.jpg"}"

I am simply trying to get this data so I can parse it with jquery.

Any ides?


It's returning JSON. Try using jQuery's getJSON() like this example:

$.getJSON('url', function(data){
    alert(data.name);
});


I see an extra " sign at the end of your line. That might be your problem. Example below works fine for me:

ajax.php

echo '{"name":"05571C-83E2A2-D4F3F0-6D7EE3-58A5F6-606843_2009-1.jpg",
"size":54823,
"type":"application\/octet-stream",
"url":"\/admin\/..\/uploads\/05571C-83E2A2-D4F3F0-6D7EE3-58A5F6-606843_2009-1.jpg",
"large_url":"\/admin\/..\/uploads\/large\/05571C-83E2A2-D4F3F0-6D7EE3-58A5F6-606843_2009-1.jpg",
"medium_url":"\/admin\/..\/uploads\/medium\/05571C-83E2A2D4F3F0-6D7EE3-58A5F6-606843-2009-1.jpg",
"thumbnail_url":"\/admin\/..\/uploads\/thumbs\/05571C-83E2A2-D4F3F0-6D7EE3-58A5F6-606843-2009-1.jpg"}';

call.php

<a href="#" id="trigger">Alert json</a>
<script>
    $('#trigger').click(function() {
        $.ajax({
          url: 'ajax.php',
          dataType:'json',
          success: function(data) {
              $.each(data, function(key, value) {
                    alert(key +":"+value); 
                });
          }
        });
    });
</script>

If you want to call just one child item from encoded json you can use it like this after success:

success: function(data) {
alert(data.name);
alert(data.size);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜