Javascript jquery ajax请求不返回页面

Javascript jquery ajax请求不返回页面,javascript,php,ajax,Javascript,Php,Ajax,我的表单将多个文本字段和一个文件上载到php脚本,以保存文件并将文本字段保存到sql db。这是可行的,我的php脚本以 ('state' => 200, "success" => true, "id" => $_POST['id'], "titlea" => $_POST['title'], "addressa" => $_POST['address'], "lot_sizea" => $_POST['lot_size'], "z

我的表单将多个文本字段和一个文件上载到php脚本,以保存文件并将文本字段保存到sql db。这是可行的,我的php脚本以

             ('state'  => 200, "success" => true, "id" => $_POST['id'], "titlea" => $_POST['title'], "addressa" => $_POST['address'], "lot_sizea" => $_POST['lot_size'], "zoninga" => $_POST['zoning'], "build_sizea" => $_POST['build_size'], "sale_pricea" => $_POST['sale_price'], "lease_pricea" => $_POST['lease_price'], "commenta" => $_POST['comment'], "transactiona" => $_POST['transaction'], "ad_linka" => $ad_link, 
             );
  exit(json_encode($response));
返回一个json编码的响应。然而,返回的内容停留在php页面上,而不是返回包含ajax请求的页面。以下是我的ajax脚本:

$("document").ready(function() {
    $(".data-form").submit(function() {
        var formData = new FormData($('form')[0]);
        if (confirm("\t\t\tAre you ready to sumbmit this listing?\nYou can always edit the listing after going to the menu tab - edit listing.")) {
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "add-list.php",
                data: formData,

                success: function(response) {
                    if (response.success) {
                        $("#modal1").modal('hide');
                        $("#add_frame").show();
                        $("#newbutt").show();
                        $(".atitle").html('<a href="' + response.ad_linka + '" target="_blank">' + response.titlea + '</a>');
                        $(".acomment").html(response.commenta);
                        $(".aaddress").html(response.addressa);
                        $(".asale-price").html(response.sale_pricea);
                        $(".alease-price").html(response.lease_pricea);
                        $(".alot-size").html(response.lot_sizea);
                        $(".abuilding-size").html(response.build_sizea);
                        $(".azoning").html(response.zoninga);
                    } else {
                        console.log("An error has ocurred: sentence: " + response.sentence + "error: " + response.error);
                    }
                },
                error: function() {
                    alert("An Error has ocurred contacting the server. Please contact your system administrator");
                }
            });
            return false;
        }
    });
});
$(“文档”).ready(函数(){
$(“.data form”).submit(函数(){
var formData=新的formData($('form')[0]);
如果(确认(“\t\t\t您准备好使用此列表了吗?\n在进入“菜单”选项卡-编辑列表”后,您始终可以编辑列表。”){
$.ajax({
类型:“POST”,
数据类型:“json”,
url:“addlist.php”,
数据:formData,
成功:功能(响应){
if(response.success){
$(“#modal1”).modal('hide');
$(“添加框架”).show();
$(“#newbutt”).show();
$(“.atitle”).html(“”);
$(“.acomment”).html(response.commenta);
$(“.aaddress”).html(response.address);
$(“.asale price”).html(response.sale_pricea);
$(“.alease price”).html(response.lease_pricea);
$(“.alot size”).html(response.lot\u sizea);
$(“.abuilding size”).html(response.build\u sizea);
$(“.azoning”).html(response.zoninga);
}否则{
log(“出现错误:句子:“+response.句子+”错误:“+response.error”);
}
},
错误:函数(){
警报(“联系服务器时出现错误。请与系统管理员联系”);
}
});
返回false;
}
});
});
我如何更改此设置,以便返回页面并允许成功方法运行

以下是我表格的开头:

<form class="avatar-form" enctype="multipart/form-data" method="POST">

耦合问题:

首先,您需要确保选择了正确的表单
$('form')
将选择所有表单,
$('form')[0]
将选择第一个表单。相反,您应该以正确的表单为目标

$('.data-form')[0]
接下来,在发送FormData时,必须将processData和contentType设置为false,以便jQuery不会尝试处理FormData。(这是在
$.ajax({…})
中完成的)

注意:ajax文件上传在IE<10上不起作用



使用
event.preventDefault
而不是返回false也是一种很好的做法(至少在调试时是这样),并在处理程序的顶部执行此操作,这样,如果出现任何语法错误,您的表单将不会提交,您将无法看到错误。请参见普雷斯顿的答案以获取示例。

评论不用于扩展讨论;这段对话已经结束。
contentType: false,
processData: false