doudi5291 2015-11-26 05:12
浏览 44
已采纳

如何在CodeIgniter中调试AJAX的非工作响应?

I want to upload photo with jquery-ajax in CodeIgniter. First I want to pass file to my controller for just check my AJAX call properly working or not. My code is not posting anything to my controller. I posting my code here, please show me my fault

Here is my code

jQuery and my input of my view is here (profile_view.php)

<script type="text/javascript">
$(document).ready(function() {
    $("#file1").on("change", (function() {
        $("#showimage").fadeIn("slow").html('');
        $("#showimage").fadeIn("slow").html('Plaese Wait...');
        alert('hello');
        $("#frm1").ajaxForm({
            target: '#showimage',
            success: function(response) {
                alert(response);
            },
            error: function(err) {
                alert(err);
            }
        }).submit();
    }));
</script>

My input code is here

<div id="photoframe">
    <form name='frm1' id="frm12" enctype="multipart/form-data" method="post" action="upload_photo">
        <input type="file" name="file1" id="file1" style="visibility:hidden" />
    </form>
    <a style="color:white"><i class="fa fa-edit" id="img1">Edit  Photo</i></a>
    <div id="showimage" name='showimage'>
    </div>
</div>

My controller is Here (upload_photo.php)

class Upload_photo extends CI_Controller {
function __construct() {
    parent::__construct();
    $this - > load - > helper(array('form', 'url'));
}
public
function index() {
    $config['upload_path'] = '/user';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';
    if (isset($_POST['frm1'])) {
        echo 'Image Uploaded';
        echo "post".$_FILE['frm1'];
    } else {
        echo 'Image Upload Failed';
    }
}
}

My output is : Image Upload Failed

  • 写回答

2条回答 默认 最新

  • dongxiequ3724 2015-11-26 05:17
    关注

    I think there is problem with your request, You have not passed any data with that request, You can use following data method to pass data with ajax:-

    $.ajax({
         url: "post.php",
         data: {
            id: 123
        },
         type: "GET",
         dataType : "json",
         success: function( json ) {
            // Do your code here for success
        },
         error: function( xhr, status, errorThrown ) {
            alert( "Sorry, there was a problem!" );
        },
         complete: function( xhr, status ) {
            alert( "The request is complete!" );
        }
    });
    

    And you can not send image file with these AJAX request.

    Please change your PHP code also, something like following :-

    if (isset($_FILES['frm1'])) {
       // Do your upload code here
    }
    else
    {
        echo 'Image Upload Failed';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)