Javascript 为什么我的AJAX请求会打开我的PHP文件,而不是像应该的那样返回响应文本?

Javascript 为什么我的AJAX请求会打开我的PHP文件,而不是像应该的那样返回响应文本?,javascript,html,jquery,ajax,web,Javascript,Html,Jquery,Ajax,Web,我正在为我的Web工程课做作业,我正在使用jQuery使用POST向我的PHP文件发出AJAX请求。分配要求“每次按下[submit]按钮时,表单信息必须显示在同一网页的显示部分,而不必重新加载页面”,这就是我使用AJAX请求的原因,但每次尝试发出AJAX请求时,它都会打开我的PHP文件,而不是像预期的那样给我一个响应。我查看了互联网上的所有资源,没有任何东西对我有帮助 我的javascript: function submit() { $.ajax({

我正在为我的Web工程课做作业,我正在使用jQuery使用POST向我的PHP文件发出AJAX请求。分配要求“每次按下[submit]按钮时,表单信息必须显示在同一网页的显示部分,而不必重新加载页面”,这就是我使用AJAX请求的原因,但每次尝试发出AJAX请求时,它都会打开我的PHP文件,而不是像预期的那样给我一个响应。我查看了互联网上的所有资源,没有任何东西对我有帮助

我的javascript:

function submit() {
            $.ajax({
                type: "POST",
                url: "assign13.php",
                data: $('#registration_form').serialize(),
                dataType: "json",
                success: function(data) {
                    var jsonResponseObj = JSON.parse(data);
                    var table_item = document.getElementById("schedule_items");
                    var string = "";
                    for (var i = 0; i < jsonResponseObj.length; i++) {
                        string += 
                            ("<tr><td>" + jsonResponseObj[i].studName0 + (jsonResponseObj[i].hasOwnProperty('studName1') ? " and " + obj[i].studName1 : "") + 
                            "</td><td>" + jsonResponseObj[i].location + 
                            "</td><td>" + jsonResponseObj[i].time + 
                            "</td><td>" + jsonResponseObj[i].performace + 
                            "</td></tr>");
                    }
                    $("#schedule_items").html(string);
                }
            });
        }

听起来好像表单正在进行本机提交,在尝试进行AJAX提交之前,您需要防止表单进行本机提交


//选择您的表单
var form=document.querySelector('form')//如果需要,将此选择器更改为更具体的选择器
//向表单添加“提交”侦听器并阻止其提交
表单.addEventListener('submit',函数(事件){
event.preventDefault()
})
编辑: 正如其他人所指出的,您可以通过添加type属性来编辑按钮以防止表单提交


Submit

你能在“assign13.php”中共享代码吗?请显示该函数是如何触发的HTML。我已经更新了我的postFYI,默认情况下,“常规按钮”是一个提交按钮。尝试从副本中添加
type=“button”
,看我甚至没有使用submit类型的输入。我正在使用按钮和onclick。如果你的
按钮是
表单中嵌套的唯一按钮,那么它将尝试提交表单。好的,我添加了你推荐的代码,现在似乎可以工作了,但出于某种原因,我仍然没有收到服务器的响应。你有一个
成功
处理程序,尝试添加一个
错误
一个。尝试使用此选项,但仍然没有得到任何结果
class festSched {
    public $studName0;
    public $studName1;
    public $location;
    public $time;
    public $performance;
}

$filename = "./data/fest_sched.json"; // storing my data as a json cause I like json

$file = fopen($filename, "w+");

$sched_json = file_get_contents($filename, true); // read the whole file into a string

$schedule = json_decode($sched_json, true);

if ($schedule == NULL) {
    $schedule = Array();
}

$new_entry = new festSched();
$new_entry->studName0 = $_POST['first_name'] . " " . $_POST['last_name'];
if ($_POST['performance'] == "duet") {
    $new_entry->studName1 = $_POST['first_name_2'] . " " . $_POST['last_name_2'];
}
$new_entry->location = $_POST['location'] . " Room " . $_POST['room'];
$new_entry->time = $_POST['time_slot'];
$new_entry->performance = $_POST['performance'] . " for " . $_POST['skill'] . " " . $_POST['instrument'];
array_push($schedule, $new_entry);

fwrite($file, json_encode($schedule));

fclose($file);



$str = json_encode($schedule);
echo $str;