在表单提交时,我使用jQuery$.ajax发出一个AJAX请求:

$('form.sendMessage').off('submit').on('submit', function (e){
            e.preventDefault();
            var form = $('form.sendMessage');
        
            if (form.valid(){
                $.ajax({
                    url: base_url + <?=$moduleUrl?> + '/sendMessage',
                    data: form.serialize();
                    type: 'POST',
                    beforeSubmit: function () {
                        if (!confirm('Are you sure you want to send this message?')) {
                            return false;
                        }
                    },
                    success: function () {
                        toastr.success("Message sent", "Success");
                    },
                    error: function (response) {
                        toastr.error(response, "Error");
                    }
                });
            }
        )
        });

在控制器中,对于发出请求的sendMessage方法,我有一个条件,如果传递该条件,将继续到sendingMessage方法:

public function sendMessage()
    {
        $post = $this->input->post(null, true);

        if ($this->input->is_ajax_request() && !empty($post)) {
            try {
                $this->sendingMessage($post);
            } catch (Exception $e) {
                $this->output->set_status_header('401')
                    ->set_output($e->getMessage());
            }
        }
    }
private function sendingMessage($post)
    {
        if (!empty($post) && (int)$post['device'] > 0 && isset($post['message'])) {
            $this->load->model('protectee/device_messages');
            // save in db
            $data = [
                'device_id' => $post['device'],
                'user_id'   => $_SESSION['UserID'],
                'message'   => $post['message'],
                'date'      => date('Y-m-d H:i:s'),
            ];
            $messageId = $this->device_messages->insertMessage($data);

            if ($messageId > 0) {
                $this->insertCommand($post['device'], 'MSG', '<br>' . $post['message']);
            } else {
                throw new Exception('Message could not be send! Please try again..');
            }
        } else {
            throw new Exception('An error occured! Please try again..');
        }
    }

当我提交表单时,它转到一个空白页面,URL更改为.../sendMessage. 因此,我认为,它不符合成为AJAX_Requestif ($this-\>input-\>is_ajax_request() && !empty($post)的条件.

推荐答案

You submit handler is not set, this is because there is a syntax error in it.
The url parameter has the issue, the php output is not a string.

url: base_url + '<?=$moduleUrl?>' + '/sendMessage',

Php相关问答推荐

未找到laravel cascadeOnEdit

Laravel有一个具有两个匹配字段

PHP Array to Powershell exec命令Args

显示WooCommerce当前产品类别或标签的短代码

PHP,ZipArchive删除一个空文件?

无法使用php IMAP从Gmail获取收件箱

如何在Livewire 3 Form类中做依赖注入?

在指定的约束内使用随机量填充数据集

未收到Strava WebHook事件数据

shell_exec运行大型数据处理

除WooCommerce中的特定国家/地区外,有条件地要求填写邮政编码 checkout 字段

wp_insert_post() 和 WordPress 中查询字符串的多个帖子

单个产品页面 WooCommerce 订阅价格字符串的更改

致命错误:未捕获 Google_Exception:客户端机密 JSON 文件无效.在 /somepath/lib/vendor/google/apiclient/src/Google/Client.php

从 Laravel 机制中排除目录并直接访问该目录

图像未在 Laravel Ajax Crud 中显示

PHP使用三元运算符进行null合并(再次)

如何通过额外的属性和本地化来使用 laravel 枚举

Docker: unixodbc.h 没有这样的文件或目录.已安装 unixodbc-dev 时出现pecl install sqlsrv错误

通过php在xml文件中添加 node