简体   繁体   English

AJAX POST成功和错误触发

[英]AJAX POST success and error firing

I checked many posts here regarding same issue but nothing was working, and more or less it behaves like a glitch: 我在这里检查了许多关于同一问题的帖子,但没有任何效果,或多或少地表现为故障:

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>

Simply i'm trying to alert the proper message if the user new to fire success or if he registered to fire error, I tried to remove dataType:'text' or adding 'json' and\\with removing crossDomain attribute as well but all didn't give me the reason. 只是我试图警告正确的消息,如果新用户触发成功或如果他注册要触发错误,我试图删除dataType:'text'或添加'json'和\\,同时也删除了crossDomain属性,但是所有操作都没有不给我原因。 In case this was useful, here is where I fire the AJAX script. 如果这很有用,请在此处触发AJAX脚本。

You call the JavaScript when you click on a submit button. 单击提交按钮时,将调用JavaScript。

The JavaScript runs. JavaScript运行。 The Ajax request is prepared. Ajax请求已准备就绪。 The JavaScript finishes. JavaScript完成。 The submit button's default behaviour submits the form. 提交按钮的默认行为将提交表单。 The page unloads. 页面卸载。 The Ajax request is canceled . Ajax请求被取消 The regular form submission takes place. 定期提交表单。

Get rid of the onclick attribute. 摆脱onclick属性。 Bind your event handlers with JavaScript instead. 而是将事件处理程序与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
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM