【问题标题】:Ajax send no data [ Codeigniter ]Ajax 不发送数据 [Codeigniter]
【发布时间】:2017-01-12 14:33:13
【问题描述】:

我在 ajax 中遇到问题,确实,我尝试在提交之前使用 ajax 将值发送到我的上传函数。 但是当我在我的 php 代码中检查 $_POST 数组时,只有表单的值,而不是来自 ajax,我不知道为什么。 这是我的代码:

HTML:

<button id="btn_saisie" class="btn btn-app saver adddocu" ><i class="fa fa-save whiter"></i></button>

<form action="/uploader/adddocu" id="form_saisie" class="form_saisie" method="POST" enctype="multipart/form-data">

<input type="file" name="document" class="val_peage form-control form_num" id="document" data-rest="document" placeholder="Document">
<input type="text" name="description" class="val_parking form-control form_num" id="description" data-rest="description" placeholder="Description">

JS:

$( ".adddocu" ).click(function() {
if ($('#document').val() != "" && $('#description').val() != ""){
    api_sendvalue_adddoc();
}
if ($('#document').val() == "")
    alert('test');
else if ($('#description').val() == "")
    alert('test2'); });


function api_sendvalue_adddoc(){
user = JSON.parse(sessionStorage.getItem('user'));
pays = localStorage.getItem("pays");
magasin = localStorage.getItem("magasin");
$.ajax({
    type: 'POST',
    url: '/uploader/adddocu',
    data: {pays:pays, magasin:magasin},
    success: function(data){
                alert(data);
                $("#form_saisie").submit();
      console.log(data);
    },
            error: function(xhr){
                alert(xhr.responseText);
                console.log(xhr.responseText);
            }
}); }

PHP:

public function adddocu(){
    $path = './asset/upload/pdf/';
    $path2 = '/asset/upload/pdf/';
    $config['upload_path']   = $path;
    $config['encrypt_name']   = false;
    $config['file_ext_tolower']  = true;
    $config['allowed_types']  = 'pdf';

    // die(var_dump($_POST));
    $this->load->library('upload', $config);
    foreach($_FILES as $id => $name)
    {
        $this->upload->do_upload('document');
        $upload_data = $this->upload->data();
        $url =  $path2 . $upload_data['file_name'];
        $data = array('nom' => $upload_data['raw_name'], 'description' => $_POST['description'], 'url' => $url, 'user_id' => '17');
        $this->db->insert('pdf', $data);
    }
    redirect("/login/docu");
}

所以,当我 var_dump $_POST 数组时,我只有“description”的值,而不是“pays”和“magasin”的值。

你能帮帮我吗?

感谢您的宝贵时间。

【问题讨论】:

  • 我猜你在进行 ajax 调用时缺少 dataType : "json" ajax 参数应该类似于 type: 'POST', url: '/uploader/adddocu', dataType : "json", data: {pays:pays, magasin:magasin},
  • 如果我添加 datatype ,该函数不起作用,它会调用错误
  • 您遇到了什么错误?确保它的 dataType 不是 datatype
  • 这是数据类型,如果我在错误函数中控制台记录 xhr,我有这个Object {readyState: 4, responseText: "array(2) {↵ ["pays"]=&gt;↵ string(2) "be"↵ ["magasin"]=&gt;↵ string(2) "fn"↵}↵", status: 200, statusText: "OK"}

标签: php jquery html css ajax


【解决方案1】:

似乎您正在访问 localstorage value ,您将其发布到某处然后提交表单。

更多你提交的表单没有这个pays & magasin 所以我有一个技巧可以用来实现它。

在您的 HTML 表单中创建两个隐藏的输入,例如

<input type="hidden" name="pays" id="pays">
<input type="hidden" name="magasin" id="magasin">

现在在 ajax 调用之前,在从本地存储中获取值后给它们值,就像这样。

user = JSON.parse(sessionStorage.getItem('user'));
pays = localStorage.getItem("pays");
magasin = localStorage.getItem("magasin");

$("#pays").val(pays);
$("#magasin").val(magasin);

$.ajax({ .... });

继续您的代码并享受。 希望它对你有用。

【讨论】:

  • 我确信我得到了报酬和 magasin,因为在成功的功能中,我使 alert(data) 并且我有价值。使用您的代码,没有任何改变,我的 $_POST 没有 ajax 值
  • 如果遇到任何错误,请在 firebug 的网络选项卡中检查 xhr 调用
  • 你试过这样吗?数据:$("#form_saisie").serialize(),
  • 是的,我试过了,我用 data.push 添加我的值,它不起作用
  • @CorentinVerpillat 请加入这个聊天室chat.stackoverflow.com/rooms/122559/laravel-5-2-talk
【解决方案2】:

编辑:

这是一个 ajax 发布到 codeigniter 的工作示例:

查看

<script> 
    $( document ).ready(function () {
      // set an on click on the button
      $("#button").click(function () {
        $.ajax({
              type: 'POST',
              url: "[page]",
              data: {pays: "asd", magasin: "dsa"},
              success: function(data){
                          alert(data);
                          $("#text").html(data);
                       console.log(data);
              },
                      error: function(xhr){
                          alert(xhr.responseText);
                          console.log(xhr.responseText);
                      }
          });

      });
    });
  </script>

控制器

<?php
    // main ajax back end
    class Time extends CI_Controller {
        // just returns time
        public function index()
        {
            var_dump($_POST);
            echo time();
        }
    }
?>

输出

array(2) {
  ["pays"]=>
  string(3) "asd"
  ["magasin"]=>
  string(3) "dsa"
}
1473087963

Working example here

因此,您应该在开发控制台上检查您从 AJAX 发出的请求。在那里你应该得到 var_dump($_POST) 的响应。

要调试尝试使您的控制器仅返回 $_POST 数据,请注释其余部分。在 javascript 方面也是一样,只测试收到的 ajax 帖子和数据。

【讨论】:

    【解决方案3】:

    问题是因为您没有阻止form 正常提交,所以AJAX 请求被取消。不要使用buttonclick 事件,而是挂钩formsubmit 事件并调用preventDefault()。试试这个:

    $('#form_saisie').submit(function(e) {
        e.preventDefault();
    
        if ($('#document').val() != "" && $('#description').val() != ""){
            api_sendvalue_adddoc();
        }
    
        if ($('#document').val() == "")
            alert('test');
        else if ($('#description').val() == "")
            alert('test2'); 
    });
    

    【讨论】:

    • 我已经尝试过这样做,但是当我这样做时,提交有一个无限循环。在我的代码中,我不必阻止提交按钮,因为它不是提交按钮,它只是一个按钮,当我点击时,我在 ajax 中进行提交>
    猜你喜欢
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 2018-04-13
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多