MAO-EYE 2017-12-07 09:48 采纳率: 100%
浏览 25

Ajax Jquery函数响应

I am learning to use jQuery Ajax. What does function(response) mean and what is the meaning of response == 1 and response == 2?

jQuery.post(ajaxurl, data, function(response) {
  if (response == 1) {
    $saveAlert.addClass('is-success').delay(900).fadeOut(700);
    setTimeout(function() {
      $tieBody.removeClass('has-overlay');
    }, 1200);
  } else if (response == 2) {
    location.reload();
  } else {
    $saveAlert.addClass('is-failed').delay(900).fadeOut(700);
    setTimeout(function() {
      $tieBody.removeClass('has-overlay');
    }, 1200);
  }
});
  • 写回答

1条回答 默认 最新

  • 妄徒之命 2017-12-07 10:37
    关注

    I'll explain the basics:

    jQuery.post = You want to post some data to your endpoint

    ajaxurl = your endpoint address. Typically a API

    data = the data you want to send to your endpoint along with the request.

    function(response) is where you handle the response from the endpoint.

    So lets go through the code. First you call post to your endpoint / API with the data you want to post. Then you provide a callback (in your case a function) to handle the response the endpoint / API provides you.

    In your case it looks like if the endpoint responds with 1, you have successfully posted your data. If the endpoint responds with 2, you have posted data and want to reload the site. If the endpoint does NOT respond with either 1 or 2, it failed to post.

    To help you understand the basics of jQuery post:

    A well-written API / endpoint should respond with the correct HTTP status codes and status messages and it should be fairly easy to notice when data was posted correctly and when an error occurred.

    I don't think your endpoint responds any good. response == 1 or response == 2is not by any means clear and easy to understand.

    A better way of handling success and errors is to use the done and fail handlers of jquery post (more on this below).


    $.post( "test.php" );
    

    This will completely ignore the response from calling the endpoint. I.e. you don't want to handle either success or error. PLEASE DO NOT USE THIS. You should handle success and error!


    $.post( "test.php", function( data ) {
      //Do something now
    });
    

    This will do whatever you provide in the function when the endpoint has sent its response.


    $.post( "example.php", function() {
      //You successfully reached the endpoint
      console.log( "success" );
    })
      .done(function() {
        //Handle success here!
      })
      .fail(function() {
        //Handle error here
      })
    

    This is probably the preferred way of handling both success and errors. It's pretty clear that if you reach the fail, it has failed. Both easy to read the code and easy to understand jquery post!


    The examples above is copied from the jquery documentation.

    Note that the previous handlers for done and fail was success and error, but success and error is deprecated and removed in jQuery 3.0!

    So if you have jQuery version < 3.0 you need to find out if you need to use the new or old syntax, or if you need to update jQuery maybe?

    Hope this explains enough to make you understand the basics, and to help you get further. I highly suggest you read the jQuery.post documentation here. I think you should also read the w3schools documentation here.

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程