【问题标题】:NULL value when post ajax curl php发布ajax curl php时的NULL值
【发布时间】:2026-02-12 04:30:01
【问题描述】:

为什么总是使用 codeigniter 中的 CURL 方法来自 Ajax 的 NULL 值数据?

这是我的错误:

发生数据库错误错误号:23000/515 [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]无法将值 NULL 插入 列'noreg',表'DBRS_MARGONO.dbo.VKLAIM_VERIF';列不 允许空值。插入失败。插入“VKLAIM_VERIF”(“noreg”, “user_id”、“已验证”、“level_verif”、“catatan”)值(NULL、NULL、 NULL, 0, '') 文件名: C:/xampp/htdocs/ws/system/database/DB_driver.php 行号:691

这是我在调用 ajax 时要打印的控制器:

$post = array();
        $post['noreg'] = $this->input->post('noreg');
        $post['user'] = $this->input->post('user');
        $post['status'] = $this->input->post('verified');

        $url = 'http://localhost/ws/rsms/verifupdatesimpanstatus';

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Content-Type: application/json; charset=utf-8",
            "X-API-Key: " . $aksesws['X-API-Key'],
            "IDAPP: " . $aksesws['IDAPP'])
        );

        $de = curl_exec($ch);
        $d = json_decode($de, true);
        curl_close($ch);
        echo $de;

但是当我测试我的帖子数据 $post 被发送时,我认为 CURL POST 方法存在问题。我一直在寻找解决方案,但我不知道错误在哪里。当我在 Postman 中使用 POST 方法 x-www-form-urlencoded

对其进行测试时,Web 服务运行良好

这是我的 ajax 代码:

function updateStatusVerif(noreg,user,verified) {
        $.post("<?php echo base_url();?>superuser/pasien/updateStatusVerif",
        {
            noreg : noreg,
            user : user,
            verified : verified
        },
        function(data){
            $('#btnverif').html(data);
        });
    }

我将来自 Web 服务的返回数据值发送到 #btnverif 元素。

【问题讨论】:

  • 向 $post 添加值后,执行 json_encode 并尝试通过邮递员发送,看看会发生什么。
  • 您的意思是在 postman 中使用 raw 代替 x-www-form-urlencoded 吗?所以我用 json 格式向 web 服务发送数据?
  • @Arthur wow bingooo,它有效,非常感谢先生

标签: php ajax codeigniter curl php-curl


【解决方案1】:

在@Arthur 的帮助下,我将json_encode 添加到curl_setopt($ch, CURLOPT_POSTFIELDS, $post);,谢谢#SOLVED

【讨论】: