douqian4411 2016-08-11 15:50
浏览 39

preventDefault()打破了外部PHP的AJAX请求

I wrote an AJAX request for uploading a file and add it's details to MySql database (by a form).

The code works great, but breaks when I add preventDefault() to the submit event (to not be directed to the external PHP file).

 $("#upload-form").submit(function(event){

            // TODO: check why breaks PHP code
            // event.preventDefault();

The same structure works great with other AJAX requests on the same file (for example AJAX request for editing MySql database).

I know the AJAX request is submitted because a new folder is created on my server (as part of the PHP code) but the file isn't uploaded.

I will be happy for an answer for one of these questions:

  1. What preventDefault() doe's that can interfere otherwise working AJAX request?
  2. How can I debug the external PHP code? as I'm not directed to it (because of the preventDefault())?

If my question is not clear enough - please help me make it clearer.

  • 写回答

2条回答 默认 最新

  • doushe8577 2016-08-11 15:58
    关注

    Since you are calling event.preventDefault() I am assuming you are sending form data with ajax.

    If you want to upload files with ajax, you need to set processData: false and contentType: false on your ajax request.

    See jQuery Ajax File Upload.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题