AJAX POST工作但回调没有被解雇

时间:2016-04-27 06:45:12

标签: javascript jquery ajax post

我查了很多关于这个问题的帖子并尝试了不同的解决方案,但没有一个对我有用,我的AJAX POST正在工作,它只是将电子邮件添加到我的电子邮件列表中,但我的回调成功和错误都未触发未知条件或我可以(随机)调用它。

HTML:

<form name="myform">
    <input id="user_email" type="email" name="email" placeholder="Your Email Here" required>
    <button type="submit" onclick="onbtnclick()">SIGN UP</button>
</form>

AJAX:

$.ajax({
    type: 'POST',
    url: 'http://mail.ayaami.com/freebook_10x10.php',
    crossDomain: true,
    data: send_data,
    success: function(responseData, textStatus, jqXHR) {

        alert("Thank you for joining Ayaami mailing list.");
        window.open("http://dev.ayaami.com/site/free-book-success");

        // var value = responseData.someKey;
        //  console.log(' log d: '+responseData);
        //  console.log(' log d: '+textStatus);
        //  console.log(' log d: '+jqXHR);
        //  var obj = jQuery.parseJSON(responseData);
        // hide the email box 
        //      text_respond="<p>"+obj.result+"</p>";

        // set cookies 

        var cookieName = 'ayaami_newsletter_lightbox';
        var cookieValue = 'true';
        var myDate = new Date();
        myDate.setMonth(myDate.getMonth() + (500 + 12));
        document.cookie = cookieName + "=" + cookieValue + ";expires=" + myDate +
            ";domain=.ayaami.com;path=/";
    },
    error: function(responseData, textStatus, errorThrown) {
        alert('You already subscribed in the mailing list.');
        console.log('log e:' + responseData);
        console.log('log e:' + textStatus);
        console.log('log e:' + errorThrown);
    }
});

我试图成功弹出警报或重定向到另一个问候页面,没有工作,我在AJAX参数之前删除了数据类型仍然没有改变。

Edition,包含document.ready

$( document ).ready(function() {    
    $('#mybtn').click(function(e){
e.preventDefault();
$.ajax({
    type: 'POST',
    url: 'http://mail.ayaami.com/freebook_10x10.php',
    crossDomain: true,
    data: {email:$(this).parent().find('input').val()},
    success: function(responseData, textStatus, jqXHR) {

        alert("Thank you for joining Ayaami mailing list.");
        window.open("http://dev.ayaami.com/site/free-book-success");

        // var value = responseData.someKey;
        //  console.log(' log d: '+responseData);
        //  console.log(' log d: '+textStatus);
        //  console.log(' log d: '+jqXHR);
        //  var obj = jQuery.parseJSON(responseData);
        // hide the email box 
        //      text_respond="<p>"+obj.result+"</p>";

        // set cookies 

        var cookieName = 'ayaami_newsletter_lightbox';
        var cookieValue = 'true';
        var myDate = new Date();
        myDate.setMonth(myDate.getMonth() + (500 + 12));
        document.cookie = cookieName + "=" + cookieValue + ";expires=" + myDate +
            ";domain=.ayaami.com;path=/";
    },
    error: function(responseData, textStatus, errorThrown) {
        alert('You already subscribed in the mailing list.');
        console.log('log e:' + responseData);
        console.log('log e:' + textStatus);
        console.log('log e:' + errorThrown);
    }
});

});

}

3 个答案:

答案 0 :(得分:5)

无论错误如何,请不要使用成功函数,请使用.done().fail().always()函数。以下代码来自JQuery文档。

var jqxhr = $.ajax( "example.php" )
  .done(function() {
    alert( "success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "complete" );
  });

// Perform other work here ...

// Set another completion function for the request above
jqxhr.always(function() {
  alert( "second complete" );
});

以下是Documentation

中的文字
  

弃用通知   jqXHR.success(),jqXHR.error()和jqXHR.complete()回调是   自jQuery 1.8起不推荐使用。准备最终的代码   删除,改为使用jqXHR.done(),jqXHR.fail()和jqXHR.always()。

答案 1 :(得分:1)

尝试以下操作,使用@Override public void onCreate(Bundle savedInstanceState) { ... // Register to receive messages. // We are registering an observer (mMessageReceiver) to receive Intents // with actions named "custom-event-name". LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("custom-event-name")); } // Our handler for received Intents. This will be called whenever an Intent // with an action named "custom-event-name" is broadcasted. private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // Get extra data included in the Intent String message = intent.getStringExtra("message"); Log.d("receiver", "Got message: " + message); //here refresh your listview // adater.notifyDatasetChanged(); } }; @Override protected void onDestroy() { // Unregister since the activity is about to be closed. LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver); super.onDestroy(); } 来阻止默认重定向到页面

<resources>
<string name="app_name">FinalDemo</string>
<string name="action_settings">Settings</string>
<string name="url">"http://10.207.201.105/apexStore2/"</string>
</resources>

ps:删除onclick功能并将按钮类型更改为按钮

String ipAddress = getApplicationContext().getString(R.strings.url);

答案 2 :(得分:0)

我们走了:

1)在您的代码中缺少订单表单序列化程序,如:

var send_data = $("#yourOrderFormId").serialize();

2)你的网址错误

3)缺少dataType,如:dataTypeL'html'或者您可以使用'php'

  $.ajax({
    type: 'POST',
    url: '/freebook_10x10.php',  
    crossDomain: true,
    data: send_data,
    dataType: 'php',
    success: function(responseData, textStatus, jqXHR) {

        alert("Thank you for joining Ayaami mailing list.");
        window.open("http://dev.ayaami.com/site/free-book-success");

        // var value = responseData.someKey;
        //  console.log(' log d: '+responseData);
        //  console.log(' log d: '+textStatus);
        //  console.log(' log d: '+jqXHR);
        //  var obj = jQuery.parseJSON(responseData);
        // hide the email box 
        //      text_respond="<p>"+obj.result+"</p>";

        // set cookies 

        var cookieName = 'ayaami_newsletter_lightbox';
        var cookieValue = 'true';
        var myDate = new Date();
        myDate.setMonth(myDate.getMonth() + (500 + 12));
        document.cookie = cookieName + "=" + cookieValue + ";expires=" + myDate +
            ";domain=.ayaami.com;path=/";
    },
    error: function(responseData, textStatus, errorThrown) {
        alert('You already subscribed in the mailing list.');
        console.log('log e:' + responseData);
        console.log('log e:' + textStatus);
        console.log('log e:' + errorThrown);
    }
});

希望它有所帮助;)