在onsubmit事件上调用ajax时出错

时间:2013-11-12 13:26:32

标签: javascript jquery ajax

我在文本字段的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' 我使用类型作为帖子。 我不想提交整份表格。 是正确的方式还是他们的替代方式? PLZ建议

2 个答案:

答案 0 :(得分:0)

尝试在return false;功能的末尾添加getTestResults

答案 1 :(得分:0)

执行此操作以使用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;
}