在提交事件上调用 ajax 时出错

Error in ajax call on onsubmit event

本文关键字:ajax 出错 调用 提交 事件      更新时间:2023-09-26

我正在对文本字段的onSubmit事件调用javascript函数这边:

  <form action="#" onsubmit="getTestResults()">
        <input class="button2" type="text" name="barcode" >
   </form> 

JavaScript函数是:

function getTestResults(){
        $.ajax({
            type: "Post",
            url : "uploadTestResult.htm",
            success : function(response) {
                alert(response);
                $("#testResultCount0").html(response);
            }
        });
    }

但是我收到错误:HTTP 状态 405 - 不支持请求方法"GET"我正在使用类型作为帖子。我不想提交整个表格。这是正确的方式还是他们的任何其他方式?请建议

尝试在getTestResults函数的末尾添加一个return false;

执行此操作以使用 AJAX 提交表单并使用 POST 发送数据

<form id="uploadForm" action="#" onsubmit="getTestResults()">
     <input class="button2" type="text" name="barcode" >
</form>
function getTestResults(){
    $.ajax({
        type: "POST",
        url : "uploadTestResult.htm",
        data: $("#uploadForm").serialize(),
        success : function(response) {
            alert(response);
            $("#testResultCount0").html(response);
        }
    });
    return false;
}