Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ajax响应错误未按预期工作_Php_Jquery_Ajax - Fatal编程技术网

Php ajax响应错误未按预期工作

Php ajax响应错误未按预期工作,php,jquery,ajax,Php,Jquery,Ajax,我的成功/错误处理未按预期工作。在本例中,我的php按预期运行,但处理的预期失败。假设我在数据库中查找用户名。。。如果我找到用户名,我将返回$ajax\u result['success']='success',但如果找不到用户名,则不会返回任何内容。成功调用工作正常,但错误调用未触发 我收到的firebug中的错误是TypeError:response为null,因此我设置的错误警报不会触发 在不实际返回$ajax_result['success']='whatever'的情况下,解决此问题的

我的成功/错误处理未按预期工作。在本例中,我的php按预期运行,但处理的预期失败。假设我在数据库中查找用户名。。。如果我找到用户名,我将返回
$ajax\u result['success']='success',但如果找不到用户名,则不会返回任何内容。成功调用工作正常,但错误调用未触发

我收到的firebug中的错误是
TypeError:response为null
,因此我设置的错误警报不会触发

在不实际返回
$ajax_result['success']='whatever'的情况下,解决此问题的最佳方法是什么

当处理按预期进行时,php返回的示例如下:

$ajax_result['success'] = 'success'; // add success info
echo json_encode($ajax_result); // return result array to ajax
对于“错误”,我只是返回:

echo json_encode($ajax_result); // which is null in this case
ajax:

var showInfo = function() {
    $('#show-user').on('click', function () {
        var $form = $(this).closest('form');
        $.ajax({
            type: 'post',
            url: '/spc_admin/process/p_delete_user_show.php',
            data: $form.serialize(),
            dataType : 'json'
        }).done(function (response) {
            if (response.success == 'success') {            
                alert('success');              
            } 
            else 
            {
                alert('error');
            }
        });
    });

}
试试这个:

  if (!response.success) {            
        alert('error');              
  } 
  else 
  {
        alert('success');
  }

您不能对空值进行
json\u编码,最简单、最干净的方法可能是将
success
设置为bool,并根据是否成功将其设置为
true
false
(这通常是大多数用户期望的“success”或“result”参数作为响应,而不是字符串)


Nothing-same
TypeError:response为null
如果您尝试通过浏览器访问php脚本,是否得到正确的响应?是否返回标题内容类型为:application/json?success调用工作正常…只有在
$ajax\u result['success'时才能执行='success';
未设置…因此我返回一个空数组,该数组将触发错误cal…或者我是这么想的。php脚本是否回显
TypeError:response是空的
还是javascript.its是js文件并指向
if(response.success='success'){
您的客户端似乎无法与服务器通信,请检查URL是否正确。
//PHP:
header('Content-Type: application/json'); // To let the client know that its json-data.
$ajax_result['success'] = $wasSuccessfull; // true or false
die(json_encode($ajax_result)); // Exit the script with the json response.


//JavaScript:
if (response.success) {            
  alert('success');              
} else {
  alert('error');
}