如何警告ajax的返回数据.

use*_*961 4 php jquery

伙计们,我有一个带有ajax的jquery页面,并在一个没有UI的单独的php文件中提交.所以我想要的是,如果说例如我的php文件中的插入查询将失败,我的php中的回声(你的插入失败)将在我的jquery页面中被警告.怎么做?

像这个警报(数据);

osh*_*nen 6

编辑2:

警告任何PHP回声:

function get_data() {
    $.ajax({
        url: 'get_data.php?rand=' + Math.random(),
        type: 'GET'
        success: function(results) { 
            alert(results);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

编辑1:

如果您希望错误出现在警报中,请执行以下操作:

对于调试ajax,你可以像这样检查xhr,status和error:

function get_data() {
    $.ajax({
        url: 'get_data.php?rand=' + Math.random(),
        type: 'GET',
        error: function(xhr, status, error) {
            alert(status);
            alert(xhr.responseText);
        },
        success: function(results) { 
            /* clear old results, then display the new results */
            $("#divResults").empty().append(results);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

但这可能并不总是显示完整的消息,特别是如果错误消息包含大量数据.它可能最终会离开屏幕.

原始答案:

对于调试ajax,你可以像这样检查xhr,status和error:

function get_data() {
    $.ajax({
        url: 'get_data.php?rand=' + Math.random(),
        type: 'GET',
        error: function(xhr, status, error) {
            /* clear old error message, then display the new php error message */
            $("#divErrorMessages").empty().append(status);
            $("#divErrorMessages").append(xhr.responseText);
        },
        success: function(results) { 
            /* clear the error message first */
            $("#divErrorMessages").empty();

            /* clear old results, then display the new results */
            $("#divResults").empty().append(results);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

在您的HTML中,您应该有2个div

<div id="divResults"></div>
<div id="divErrorMessages"></div>
Run Code Online (Sandbox Code Playgroud)


diE*_*cho 0

尝试这样

$.ajax({
  url: "/post/post_url.php",
  type: "POST",
  data: parameters,
  success: function(){
    alert('success');
  },
  error: function(){
    alert('failure');
  }
});
Run Code Online (Sandbox Code Playgroud)

参考

  • 我认为不是这样,因为PHP脚本执行时没有错误(逻辑有错误,而不是过程),那么返回代码将始终是“成功”。 (2认同)