响应/成功/失败(AJAX / JSON + PHP)

时间:2019-03-05 03:27:47

标签: php json ajax

我有一个.JS文件。

如何根据.PHP文件中的不同变量赋予data['status']值? (如果需要,我需要添加一些内容)。正如您在以下脚本中看到的那样,我需要显示不同的失败消息:

$.ajax({
      type: 'POST',
      dataType: 'json',
      url: '/dashboard/check/',
      data: {
        insta_user: insta_u,
        insta_country: insta_country_code,
      },
      success: function (response) {
        console.log(data);
        if (data['status']) {

          $.ajax({
            type: 'POST',
            dataType: 'json',
            url: '/dashboard/action-add-account.php',
            data: {
              insta_user: insta_u,
              insta_pass: insta_p,
              insta_country: insta_country_code,
            },
            success: function (data) {
              got_response = true;
              if (data['status']) {
                setTimeout(function () {
                  $.dialog({
                    title: 'Done',
                    content: "Now you can edit your activity",
                    icon: 'fab fa-instagram',
                    theme: 'modern',
                    closeIcon: true,
                    animation: 'scale',
                    type: 'green',
                  });
                }, 200);
                setTimeout(function () {
                  location.reload();
                }, 4500);
              }
              else if (data['step_two']) {

                setTimeout(function () {
                  $.dialog({
                    title: 'Great',
                    content: "Follow steps",
                    icon: 'fas fa-check',
                    theme: 'modern',
                    closeIcon: true,
                    animation: 'scale',
                    type: 'green',
                  });
                }, 200);
                setTimeout(function () {
                  location.reload();
                }, 4500);

              }
              else {
                if (data['error']) {

                  var error_title = 'Error!';
                  if (data['error_title']) {
                    error_title = data['error_title'];
                  }

                  setTimeout(function () {
                    $.dialog({
                      title: error_title,
                      content: data['error'],
                      icon: 'fas fa-exclamation-triangle',
                      theme: 'modern',
                      closeIcon: true,
                      animation: 'scale',
                      type: 'red'
                    });
                  }, 200);

                }

                else {
                  setTimeout(function () {
                    $.alert('Server error, try again later.');
                  }, 200);

                }
              }
              setTimeout(function () {
                location.reload();
              }, 6000);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
              got_response = true;
              $.alert('Server error, try again later.');
              setTimeout(function () {
                location.reload();
              }, 4000);
            }
          });

        }
        else {
          if (data['error'] == 'country') {
            got_response = true;
            setTimeout(function () {
              $.dialog({
                title: 'Choose your country',
                content: "We need to set your country first!",
                icon: 'fas fa-exclamation-triangle',
                theme: 'modern',
                closeIcon: true,
                animation: 'scale',
                type: 'red'
              });
            }, 200);
            setTimeout(function () {
              location.reload();
            }, 4000);
          }
          else if (data['error'] == 'exists') {
            got_response = true;
            setTimeout(function () {
              $.dialog({
                title: 'Existent user',
                content: "We already have an account with that name/email",
                icon: 'fas fa-exclamation-triangle',
                theme: 'modern',
                closeIcon: true,
                animation: 'scale',
                type: 'red'
              });
            }, 200);
            setTimeout(function () {
              location.reload();
            }, 4000);
          }
          else {
            got_response = true;
            setTimeout(function () {
              $.dialog({
                title: 'Unknown user',
                content: "There's no accounts with that name.",
                icon: 'fas fa-exclamation-triangle',
                theme: 'modern',
                closeIcon: true,
                animation: 'scale',
                type: 'red'
              });
            }, 200);
            setTimeout(function () {
              location.reload();
            }, 4000);
          }
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        got_response = true;
        $.alert('Server error, try again later.');
        setTimeout(function () {
          location.reload();
        }, 4000);
      }
    });
  });
});

例如:

如果我在.PHP文件中的查询为else if (data['error'] == 'exists') {,我该如何接收$sql_un = mysql_query("SELECT * FROM users WHERE Email='".$_POST['Email']."'"); $num_un = mysql_fetch_array($sql_un); if($num_un == 0){

谢谢!

0 个答案:

没有答案