【问题标题】:My AJAX function keeps getting error as result结果我的 AJAX 函数不断出错
【发布时间】:2020-07-16 16:53:02
【问题描述】:

我是使用 AJAX 或 jQuery 的新手,我正在尝试在不加载另一个页面的情况下在表格中插入一些数据,但我不能。

这是我的 AJAX 函数: 编辑:

function SumbitAction(act_id) {
$.ajax({
dataType: 'json',
type: "POST",
url: 'Accion.php',
data: {Action:act_id},
success: function (obj, textstatus) {
              if(obj.status==='FAIL') {
                   console.log(obj.message);
              }
              else {
                  console.log("ok");
              }
        }
});
}

在我的 PHP 中,我目前没有提供任何参数来测试完整的空插入查询:编辑:

    $consultaAcciones= "INSERT INTO acciones VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL)";
$ejecAcciones = mysqli_query($conn, $consultaAcciones);
if($ejecAcciones===false){
    $response = [
'status' => 'FAIL',
        'message' => 'Accion no registrada correctamente'
    ];
}
else {
    $response = [
        'status' => 'OK',
        'message'=> 'Accion registrada'
    ];
}

header("Content-type: application/json");
echo json_encode($response);

我在控制台日志中收到“错误”,但我不知道出了什么问题。编辑:不再

编辑: 现在它没有显示任何错误,没关系,但是当一切正常时也没有显示我的控制台日志,还收到一条警告消息:“Cross-Origin Read Blocking (CORB) blocked cross-origin response” 和一个日志:XHR 完成加载:POST "http://url..."。 以这种方式指定我的函数的调用者可能不正确吗?

    function addListener(id,num)
{
  var target= document.getElementById(id);
  if(target){
    target.addEventListener("click",SumbitAction(num),false);

  }
  else{
    //console.log("Nel",id);
  }
}

【问题讨论】:

  • 在 PHP 中开启错误报告 - stackoverflow.com/a/21429652/296555.
  • 如果您想查看您的 PHP 代码中发生的任何错误,您可以试试这个。 <?php error_reporting(E_ALL); ini_set("display_errors", 1) ?>。你试试echo
  • 另外,默认情况下,Ajax 通过称为error 的方法接收错误。你可以试试这个。 error: function (request, status, error) { alert('Code:' + request.status + '\n' + 'message:' + request.responseText + '\n' + 'error:' + error); }.
  • 在代码中使用die(mysqli_error($conn)); 是一个非常糟糕的主意,因为它可能会泄露敏感信息。更多解释见这篇文章:mysqli or die, does it have to die?

标签: javascript php jquery mysql ajax


【解决方案1】:

代码中有2个问题。

首先,if(('error')) { 将始终评估为 true'error' 只是一个字符串,即truthy

您可能打算将该字符串与某些东西进行比较,也许(也删除双括号):

if (textstatus === 'error') {

接下来,textstatus请求 的状态,而不是您的代码的状态。例如,当有来自服务器的 404 或 500 响应时,它将是 error。如果你的INSERT失败了,http请求还是会成功,textstatus就不是error了。

如果您想添加一个,您可能需要在error() 回调中检查textstatus。但在success() 回调中,您可能想要检查您的代码 返回的内容。在本例中,即为obj

但是看看 PHP,它并没有返回 JS 可以使用的任何可预测的东西。如果查询有效,它不会返回任何内容。如果失败,它会返回一个以Error 开头的字符串,但随后会显示一个 MySQL 错误,我们不会提前知道,因此您无法在 JS 中对其进行测试。

最好简单地返回一些真/假,OK/失败类型的msg,例如:

// ... rest of your code
$result = mysqli_query($conn, $consultaAcciones);
if ($result === false) {
    echo "FAIL";
} else {
    echo "OK";
}

在你的 JS 中:

if (obj === 'FAIL') {
// ... etc

如果你真的想从 PHP 传回消息,你应该让它回显一个 JSON 对象,例如:

$result = mysqli_query($conn, $consultaAcciones);
if ($result === false) {
    $response = [
        'status' => 'FAIL',
        // You shouldn't reveal internal details about errors
        // so don't return mysqli_error().  Maybe log it, and 
        // display something generic to your users.
        'message' => 'There was a problem'
    ];
} else {
    $response = [
        'status' => 'OK',
        'message'=> ''
    ];
}

header("Content-type: application/json");
echo json_encode($response);

在你的 JS 中:

$.ajax({
    // ...
    // specify we will get a JSON response
    dataType: 'json'
    // ...
    success: function (obj, textstatus) {
        if (obj.status === 'FAIL') {
            alert(obj.message);
        } else {
            // all ok ...
        }
});

【讨论】:

  • 对此我真的很抱歉,我投了赞成票,但因为我是新人,所以我的赞成票没有;不会被记录下来。
  • @AlejandoMaldonado 好的,感谢您的回复 :-) You can also accept 如果这回答了您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
相关资源
最近更新 更多