Javascript AJAX请求-检查操作是否成功

Javascript AJAX请求-检查操作是否成功,javascript,jquery,html,Javascript,Jquery,Html,我有一个Javascript,它是从一个发出HTTP GET请求的按钮调用的。当它遇到错误时,它会显示一个隐藏的div和请求错误,这一切都很正常。以下是脚本: $(“#呼叫联系人1”)。单击(函数(){ console.log(“启动事件”); $.ajax({ url:“+eventID+”, 数据:{}, 键入:“获取” }) .then(功能(数据、状态、xhr){ $('#ajaxsresponse1').html(data.show(); var httpStatus=状态; var

我有一个Javascript,它是从一个发出HTTP GET请求的按钮调用的。当它遇到错误时,它会显示一个隐藏的div和请求错误,这一切都很正常。以下是脚本:

$(“#呼叫联系人1”)。单击(函数(){
console.log(“启动事件”);
$.ajax({
url:“+eventID+”,
数据:{},
键入:“获取”
})
.then(功能(数据、状态、xhr){
$('#ajaxsresponse1').html(data.show();
var httpStatus=状态;
var httpResponseCode=(xhr.status);
log('httpStatus:'+httpStatus);
log('httpResponseCode:'+httpResponseCode);
})
.失败(功能(xhr){
var httpStatus=(xhr.status);
var httpResponseCode=(xhr.getAllResponseHeaders);
var ajaxError='存在请求事件的错误。HTTP状态:'+httpStatus;
log('httpStatus:'+httpStatus);
log('httpResponseCode:'+httpResponseCode);
//使警报可见
$('#ajaxResponse1').html(ajaxError.show();
})

})
JQuery AJAX中有一个成功函数:

像这样使用它:

.success(function(response) {
   //DO stuff here.
})
更好更简单的代码可以是:

        $.ajax({
            url: 'http://example.com',
            method: 'GET',
            success: function (response) {

            },
            error: function (e) {

            }
        });
查看jQueryAjax函数的完整文档位于


您正在用Ajax调用注册两个回调。您似乎知道
fail
是在出错时执行的。这就留下了
。然后在成功时执行
回调。只需在此处添加通话:

.then(function(data, status, xhr) {
  $('#ajaxResponse1').html(data).show();
  $('#ajaxResponseSuccess1').html('Event Update in Progress').show(); // <--
  // ...
})
.then(函数(数据、状态、xhr){
$('#ajaxsresponse1').html(data.show();

$('#ajaxResponseSuccess1').html('Event Update in Progress').show();//是否尝试添加
.done(函数(数据)){…
?您似乎知道执行
fail
回调时会显示错误消息。您猜成功后会执行哪个函数吗?我没有否决,但为什么要在OP已经使用
时建议
success
error
。然后
fail
?一种更简单的方法对于OP,他说他是JQueryI的新手,如果你要参考文档,为什么你要参考w3schools而不是实际文档:?
$.ajax({
      url: "<?php echo $eventURL ;?>" + eventID + "<?php echo $eventURL ;?>",
      data: {},
      type: "GET",
      success : function(data)
      {
        $('#ajaxResponseSuccess1').html('Event Update in Progress').show();
      },
      error:function(xhr,status)
      {
         alert(xhr.statusText);
      }
    });
  $.get("www.xyz.com/abc",{eventId: eventId},callbackFunction);