AJAX POST成功并错误触发

AJAX POST success and error firing

本文关键字:错误 POST 成功 AJAX      更新时间:2023-09-26

我在这里查看了许多关于同一问题的帖子,但什么都不起作用,或多或少它的行为就像一个小故障:

function onbtnclick(){

    var user_email=$('#user_email').val();
    var send_data = {email:user_email};
$.ajax({
        type: 'POST',
        url: 'someURL.php',
        crossDomain: true,
        data:send_data,
        dataType: 'text',
        success: function(responseData, textStatus, jqXHR) {
            alert("Thank you for the mailing list");
        },
        error: function (responseData, textStatus, errorThrown) {
            alert('Your email is already in our mailing list.');
            console.log('log e:'+responseData);
            console.log('log e:'+textStatus);
            console.log('log e:'+errorThrown);
        }
    });
        return false;
 }
};

<form name="myform">
    <input  id="user_email" type="email" name="email" placeholder="Your Email Here" style="width:300px" required><br>
    <br><button type="submit" class="leka-button button-style2 offer_button" onclick="onbtnclick()"><h5>SIGN UP</h5></button>
</form>

简单地说,如果新用户成功触发或注册触发错误,我试图提醒正确的消息,我试图删除dataType:'text'或添加'json'和'',同时删除crossDomain属性,但所有这些都没有告诉我原因。如果这是有用的,这里是我启动AJAX脚本的地方。

单击提交按钮时调用JavaScript。

JavaScript运行。Ajax请求已准备好。JavaScript结束。提交按钮的默认行为提交表单。页面卸载。Ajax请求被取消。定期提交表格。

去掉onclick属性。请改用JavaScript绑定事件处理程序。

$(function () {
    var $form = $([name="myform"]); // `name` is obsolete for form elements, give it an ID instead
    $form.on('submit', onbtnclick);
    function onbtnclick(event) { // Give this function a better name
        event.preventDefault(); // Don't submit the form normally
        // Then put the rest of your existing code
    }
});