AJAX请求失败,表单使用php ajax发送数据

用户名

我需要使用发送表单数据ajax

HTML:

<div class="" id="ajax-msg1"></div>
<form id="ajaxform" action="load.php">
   <input type="hidden" name="csrf_token" id="my_token" value="<?php echo $token; ?>" />
   <button type="submit" name="submit" id="ajax-1">Send</button>
</form>

JS:

$(document).ready(function() {
    $("#ajax-1").click(function() {
        $("#ajax-msg1").html("<img src='loading.gif'/>");
        var formData = $("#ajaxform").serializeArray();
        var URL = $("#ajaxform").attr("action");
        $.ajax({
            url: URL,
            type: "POST",
            data: formData,
            success: function(data, textStatus, jqXHR) {
                $("#ajax-msg1").html('<pre><code class="prettyprint">' + data + '</code></pre>');
            },
            error: function(jqXHR, textStatus, errorThrown) {
                $("#ajax-msg1").html('<pre><code class="prettyprint">AJAX Request Failed<br/> textStatus=' + textStatus + ', errorThrown=' + errorThrown + '</code></pre>');
            }
        });
    });
});

但在实际操作中,我看到此错误:

AJAX Request Failed

而不是工作表。如何解决此错误?

斯威德曼

就像“ Norlihazmey Ghazali”所说

改变:

$(document).ready(function() {
    $("#ajax-1").click(function() {

到:

$(document).ready(function() {
    $("#ajax-1").click(function( e ) {
        e.preventDefault();// avoid submitting the form here

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章