【问题标题】:Display popup alert after Ajax responseAjax 响应后显示弹出警报
【发布时间】:2019-08-17 15:00:50
【问题描述】:

我有一个 JSON 请求,在这段代码中使用了 ajax 的 post 方法

$(document).ready(function() {
  $(document).on('submit', '#registration_check', function() {
    var data = $(this).serialize();
    $.ajax({
      type: 'POST',
      url: 'apidomain.com',
      data: data,
      success: function(data) {

        $("#registration_check").fadeOut(500).hide(function() {
          $(".result_1").fadeIn(500).show(function() {
            $(".result_1").html(data);
          });
        });
      }
    });
    return false;
  });
});

响应将在此元素上返回 3 个字段,例如姓名、电子邮件、电话:

<div id="result_1"></div>

到目前为止它工作得很好,但我想要做的是,如果 ajax 响应发现一些返回值为 null,我想显示警报或弹出消息。例如:

  1. 如果 JSON 响应返回: 姓名:jhon,邮箱:jhon@doe.com,电话:123456789 用户将重定向到另一个页面(目前已完成)

  2. 但是如果 JSON 响应返回 姓名:jane,电子邮件:jane@doe.com,电话:

将出现弹出窗口或警报,文本电话号码为空。

【问题讨论】:

  • 显示你的php代码
  • 您好,PHP代码在处理请求的apidomain下。在用户方面,我使用带有 Ajax 的 html 作为上面的引用

标签: javascript php json ajax


【解决方案1】:

我想到的第一件事:您是否需要文档元素中的 JSON 响应,或者您不知道如何使用 jQuery Ajax?

无论如何,此解决方案在这两种情况下都应该对您有所帮助:

$(document).ready(function()
{   
    $(document).on('submit', '#registration_check', function()
    {       
        var data = $(this).serialize();
        $.ajax({
        type : 'POST',
        url  : 'apidomain.com',
        data : data,
        dataType: 'json', // tell jQuery, that the response data will be a JSON - it will be parsed automatically
        success :  function(data)
                   {
                        // now you have a parsed JSON object in the 'data' var

                        var show_alert = false;

                        if (data.phone === null || !data.phone.length) {
                            show_alert = true;
                        }

                        if (data.email === null || !data.email.length) {
                            show_alert = true;
                        }

                        if (show_alert) {
                            alert('here is the alert :)');
                        }

                        $("#registration_check").fadeOut(500).hide(function()
                        {
                            $(".result_1").fadeIn(500).show(function()
                            {
                                $(".result_1").html(JSON.stringify(data));
                            });
                        });

                   }
        });
        return false;
    });
});

【讨论】:

    【解决方案2】:

    如果您的数据是 JSON 对象,那么您可以这样做:

    success: function(data) {
      for (var i in data) {
        if (!data[i]) {
          alert(/* MESSAGE HERE */)
          return;
        }
      }
    
      // Your regular code here
    }
    

    【讨论】:

      【解决方案3】:

      您可以在 php 文件中创建一个值的关联数组,并以 json 格式回显它,例如

      echo json_encode($array);
      

      然后你会在你的 ajax 响应中收到这个

        var objs = JSON.parse(data);
      

      然后您可以通过您在 php 文件中的关联数组中定义的键解析值,如 nameemailphone

      console.log(objs.name);
      console.log(objs.email);
      console.log(objs.phone);
      

      这是您可以单独解析值的方式。您也可以通过自己的方式申请条件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-05
        • 1970-01-01
        • 2021-02-11
        • 2020-02-22
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        相关资源
        最近更新 更多