dongyuan1870 2013-08-28 19:16
浏览 23

不能使用php返回的数组[复制]

This question already has an answer here:

I am getting an array returned from PHP which I json_encode() in php first and I echo that array. I get the array with an AJAX request disabling "Async". I know I shouldn't use that but it was the only way I could find.

It returns me this:

{"id":"38","name":"111111111111111111111111111111111111111111111.jpg"}

And this is my AJAX request:

function uploadFile(file){
    var formData = new FormData();
    formData.append('formData', file);
    $.ajax({
        url: 'inc/ajax/uploadFile.php',  //Server script to process data
        type: 'POST',
        data: formData,
        contentType: false,
        processData: false,
        async:   false,
        //Ajax events
        success: function(html){
            strReturn = html;
        }
    });
    return strReturn;
}

When I do this I get the whole array:

var img = uploadFile(file);
console.log(img);

But when I call "img.name" or img.id" it says undefined.

</div>
  • 写回答

3条回答 默认 最新

  • doujianchao7446 2013-08-28 19:19
    关注

    You're receiving back a JSON string representation of an object. Tell jQuery that you're expecting JSON, so that it parses it into an actual Javascript object for you:

    data: formData,
    dataType: 'json', // Add this line
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入