简体   繁体   English

如何在ajax中放置双变量数据

[英]how to put double variable data in ajax

i've case to use variable more the one like code bellow : 我案例更多地使用变量,例如下面的代码:

 function upload(iddata,ring) { //variable 1 dataString ="iddata="+iddata+"&ring="+ring+""; // variable 1 //variable 2 var formData = new FormData(); for (var i = 0; i < filesToUpload.length; i++) { var file = filesToUpload[i]; formData.append("file[]", file, file.name); } // i want put both variabel in ajax like this $.ajax({ data: {formData,dataString}, //variable 1 and 2 in group }); } 

please how source is True 请问来源如何真实

You should handle your whole data as an object only, and not mix it as a string and a array in a object . 您应该只将整个data作为一个object处理,而不能将其作为stringarrayobject In this case you would not get the expected data in your receiver. 在这种情况下,您将无法在接收器中获得预期的数据。

You could change it like this: 您可以这样更改:

function upload(iddata, ring) {
    var data = {
        iddata: iddata,
        ring: ring,
        formData: new FormData()
    };

    for (var i = 0; i < filesToUpload.length; i++) {
        var file = filesToUpload[i];
        data.formData.append("file[]", file, file.name);
    }

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

 <?php defined('BASEPATH') OR exit('No direct script access allowed'); class MyBird extends CI_Controller { /** * Index Page for this controller. function add_bird() { // get user id posted $iddata=empty($_POST['iddata']) ? '' : $_POST['iddata']; $ring=empty($_POST['ring']) ? '' : $_POST['ring']; //echo json_encode(array('uploaded' => 'Oke Bos')); if ( ! empty($_FILES)) { $config['upload_path'] = "./assets/images/uploads"; $config['allowed_types'] = 'gif|jpg|png|mp4|ogv|zip'; $this->load->library('upload'); $files = $_FILES; $number_of_files = count($_FILES['file']['name']); $errors = 0; // codeigniter upload just support one file // to upload. so we need a litte trick for ($i = 0; $i < $number_of_files; $i++) { $Random_Number = rand(0, 9999999999); //Random number to be added to name. $NewFileName = "Ring".$ring._.$Random_Number; //new file name $config['file_name'] = $NewFileName; $_FILES['file']['name'] = $files['file']['name'][$i]; $_FILES['file']['type'] = $files['file']['type'][$i]; $_FILES['file']['tmp_name'] = $files['file']['tmp_name'][$i]; $_FILES['file']['error'] = $files['file']['error'][$i]; $_FILES['file']['size'] = $files['file']['size'][$i]; // we have to initialize before upload $this->upload->initialize($config); if (! $this->upload->do_upload("file")) { $errors++; } } if ($errors > 0) { echo $errors . "File(s) cannot be uploaded"; } } } } 

in controller i put code like bellow, but the filen can't move upload 在控制器中,我输入了类似波纹管的代码,但文件无法移动上传

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM