AJAX致命错误后列不能为空 [英] AJAX Post Fatal Error Column Cannot Be Null

查看:117
本文介绍了AJAX致命错误后列不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个致命错误,列不能为空."

I'm getting a fatal error, "Column cannot be null."

<head>
  <script>
    let xhr = window.XMLHttpRequest
      ? new XMLHttpRequest()
      : new ActiveXObject('Microsoft.XMLHTTP')
    xhr.open('POST', 'ajax.php', true)
    xhr.setRequestHeader('content-type', 'applications/x-www-form-urlencoded')
    xhr.send('sld=testing&tld=com&registrar=namecheap')
  </script>
</head>

ajax.php

var_dump($_POST);    // without these var_dumps
var_dump($_REQUEST); // i receive a 500 error (internal server error)

if ($_SERVER["REQUEST_METHOD"] === "POST"){
  include "includes/connect_to_database.php";
  $pdo = $connection->prepare("INSERT INTO domains (sld, tld, registrar) VALUES (:sld, :tld, :registrar)");
  $pdo->execute(array(
    ":sld" => $_REQUEST["sld"],
    ":tld" => $_REQUEST["tld"],
    ":registrar" => $_REQUEST["registrar"]
  ));
  $connection = null;
}

开始:connect_to_database.php

try {
  $connection = new PDO("mysql:host=$dbhost; dbname=$dbname; charset=utf8", $dbuser, $dbpass);
  $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Connected successfully!";
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage() . ".";
}

MySQL表架构

  • 它说罪魁祸首列是sld,所以好像没有任何数据向前传递.
  • responseText也是空白.
  • 我也使用$_POST得到了完全相同的错误.
  • var_dump($_REQUEST)var_dump($_POST)都在响应中返回空数组.
  • 我曾尝试在帖子字符串上使用encodeURIComponent,但这没有用.
  • 值得注意的是,如果删除var_dump语句,则会收到500错误(内部服务器错误).
  • It says the culprit column is sld, so it's as if none of the data is being passed forward.
  • The responseText is also blank.
  • I get the exact same error using $_POST too.
  • Both var_dump($_REQUEST) and var_dump($_POST) return empty arrays in the response.
  • I've tried using encodeURIComponent on the post string, and that didn't work.
  • Notably, I receive a 500 error (internal server error) if I remove the var_dump statements.

是不是我没有发送所有必要的标头?

对于不转发数据我到底在做错什么?

推荐答案

您使用了错误的请求标头. 将其更改为

You are using wrong request header. Change it to

xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')

代替

applications/x-www-form-urlencoded 在这里注意一个"s".

applications/x-www-form-urlencoded notice one 's' here.

我认为您可能不需要 $_POST$_REQUEST同时.因此,您可以删除其中之一.

I think you may not require $_POST and $_REQUEST same time. So you may remove one of them.

这篇关于AJAX致命错误后列不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆