簡體   English   中英

AJAX POST成功和錯誤觸發

[英]AJAX POST success and error firing

我在這里檢查了許多關於同一問題的帖子,但沒有任何效果,或多或少地表現為故障:

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
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM