weixin_33713503 2017-11-09 13:26 采纳率: 0%
浏览 33

$ .ajax和php响应

When i make requests this way i never get the response to manipulate . Well i get as response ***Array ( [name] => Mastering React Native (2).pdf [type] => application/pdf ... but i need to show to user the file info.

  <form id='formid'>
        <input id='fileid' type='file' name='file1' hidden/>
        <input type='submit' value='IMPORT'/>
    </form>
    <script>
        $(document).on("submit", "form", function (e) {
            e.preventDefault();
            var fdat = new FormData(this);
            fdat.append('operation', 'IMPORT');
            function importFile() {
                return $.ajax({
                    url: "data-source/oracle_controller_upload.php",
                    type: 'POST',
                    dataType: "JSON",
                    data: fdat,
                    processData: false,
                    contentType: false,
                    success: function(response){
                                   }
                });
            }
            debugger;
            importFile().done(function (result) {
            }).fail(function () {
            });
        });
    </script>

this is my php code:

if (@$_REQUEST['operation'] === "IMPORT" && $_FILES) {
    echo print_r($_FILES['file1']);
    return;
}

Cant reach either the success callback or done(). Any help?

  • 写回答

2条回答 默认 最新

  • helloxielan 2017-11-09 13:46
    关注

    Solved with

    if (@$_REQUEST['operation'] === "IMPORT" && $_FILES) {
      echo json_encode($_FILES['file1']);
      return;
    }
    

    I have dataType: "JSON", so of course the response must be JSON.

    评论
  • Didn"t forge 2017-11-09 13:47
    关注

    You haven't said exactly, but I think you must be getting an error about an invalid JSON response. Your ajax request is expecting JSON back from the server, but you're just sending a raw string dump of your variable.

    In the PHP, do this:

    if (@$_REQUEST['operation'] === "IMPORT" && $_FILES) {
        echo json_encode($_FILES['file1']); //encode the object as JSON instead of just a variable dump
        return;
    }
    

    and in your .done() method:

    importFile().done(function (result) {
      alert(result.name); //just for example, to display the name in an alert
      //...etc
    
    评论

报告相同问题?

悬赏问题

  • ¥80 关于海信电视聚好看安装应用的问题
  • ¥15 vue引入sdk后的回调问题
  • ¥15 求一个智能家居控制的代码
  • ¥15 ad软件 pcb布线pcb规则约束编辑器where the object matpcb布线pcb规则约束编辑器where the object matchs怎么没有+15v只有no net
  • ¥15 虚拟机vmnet8 nat模式可以ping通主机,主机也能ping通虚拟机,但是vmnet8一直未识别怎么解决,其次诊断结果就是默认网关不可用
  • ¥20 求各位能用我能理解的话回答超级简单的一些问题
  • ¥15 yolov5双目识别输出坐标代码报错
  • ¥15 这个代码有什么语法错误
  • ¥15 给予STM32按键中断与串口通信
  • ¥15 使用QT实现can通信