Ajax:如何解决每 _POST 美元的未定义索引错误?[复制]

Ajax: How to solve undefined index error for every $_POST? [duplicate]

提问人:Mitca Vicentiu 提问时间:5/22/2018 最后编辑:Karlo KokkakMitca Vicentiu 更新时间:5/22/2018 访问量:563

问:

我有一个.每次调用它时,我都会收到每个元素的未定义索引错误,最后我收到另一个错误:.ajaxajaxdataajaxCannot modify header information - headers already sent by

在检查器的网络字段中,我可以看到两种情况。提交后,它只会向服务器发出一个请求,file = edit_product(这将具有状态代码 200),或者将有 2 个请求:一个具有状态代码 200,一个根本没有状态代码。状态码为 200 的是由文档引起的,没有状态码的是由 xhr 引起的。

在情况 2(当有 2 个请求时)该函数将起作用。

在情况 1(当只有 1 个请求时)该函数将不起作用。

它可能是重复的,但我不这么认为。我在stackoverflow和其他论坛上搜索了很多...

阿贾克斯:

$.ajax({
                method:"POST",
                url: "<?php echo base_url('products/edit_product'); ?>",
                dataType: "json",
                data: {
                    prd_id: id,
                    prd_name: name,
                    prd_cat: category,
                    prd_ing: ingredients,
                    prd_grams: grams,
                    prd_price: price,
                    prd_show: show,
                    prd_img: img
                },
                success:function(resp){
                    if(resp.done){
                        console.log('its working');
                    }
                },
                error: function(xhr, ajaxOptions, thrownError){
                    console.log('eroare ' + thrownError);
                }
            });

控制器功能:

public function edit_product(){
    if(!$this->session->userdata('logged_in')){
        redirect('users/login');
    }
    $id = $_POST["prd_id"];
    $test = array(
        "product_name" => $_POST['prd_name'],
        'category' => $_POST['prd_cat'],
        'ingredients' => $_POST['prd_ing'],
        'grams' => $_POST['prd_grams'],
        'price' => $_POST['prd_price'],
        'show' => $_POST['prd_show'],
        'image' => $_POST['prd_img'],
    );
    echo json_encode(array(
        'done' => true,
    ));
    $config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/quartiere/assets/images';
    $config['allowed_types'] = 'jpg|png|jpeg|JPG|JPEG';
    $config['max_size'] = '0';
    $config['max_width'] = '10000';
    $config['max_height'] = '10000';

    $this->load->library('upload',$config);

    if(!$this->upload->do_upload()){
        $errors = array('error' => $this->upload->display_errors());
        $food_image = 'noimage.jpg';
    }else{
        $data = array('upload_data' => $this->upload->data());
        $food_image = $_FILES['userfile']['name'];
    }
    $this->food_model->edit_product($id,$test);
    $this->session->set_flashdata('product_edited','Ai editat produsul cu success!');
}
javascript php ajax codeigniter undefined-index

评论

0赞 WillardSolutions 5/22/2018
在访问之前测试 $_POST 密钥。在 php 中使用类似的东西来避免未定义的索引错误,这样您就可以处理错误或填写默认值array_key_exists()
0赞 deceze 5/22/2018
我正确地假设请求是以 JSON 形式发送的,是吗?然后 PHP 不会自动填充数组,因为 PHP 不能/不会隐式解析 JSON 请求正文。$_POST
0赞 Mitca Vicentiu 5/22/2018
在这种情况下,我该怎么办?我真的不明白这是怎么回事
0赞 deceze 5/22/2018
阅读顶部的重复项。
0赞 Mitca Vicentiu 5/22/2018
我读了它,但我不明白发生了什么,也不明白我该怎么办-.-

答: 暂无答案