AJAX发送空对象

时间:2019-03-24 08:12:26

标签: javascript php jquery ajax

我是一名Web开发人员,但所有人都有一个他无法纠正的错误。 这是我创建新表单以向网站添加商品 我发送ajax请求,但返回空数组

我的JS

(function(a){
    let req = {};
    req.url = "/action/do/done";
    req.type = "POST";
    req.dataType = "html";
    a(".file").change(()=>{
        let fileList = $(".file")['0'].files, fileCount = fileList.length;
        if (fileCount < 10 && fileCount > 0) {
            a('.spanText').html(fileCount + " &#351;&#601;kil se&#231;diniz...");
            $(fileList).each((i,el) => {
                let data = new FormData();
                data.append('section', 'general');
                data.append('action', 'previewImg');
                data.append('type', 'loadImage');
                data.append('image', fileList[i]);
                req.contentType = !1;
                req.processData = !1;
                req.data = data;
                req.success = (e) => {
                    console.log(e);
                    if (i === 0) {
                        a(".file").attr('datasrc', e);
                    } else {
                        a(".file").attr('datasrc', a(".file").attr('datasrc')+';'+e);
                    }
                }
                a.ajax(req);
            });
        } else if (fileCount > 10) {
            a('.spanText').html("Bir balaca &#231;ox &#351;&#601;kil se&#231;diniz.");
        } else {
            a('.spanText').html("&#350;&#601;kil se&#231;in...");
        }
    });
    a(".good-colors select").change(()=>{
        a(".good-colors select").css('backgroundColor', $(".good-colors select")['0'].selectedOptions['0'].attributes['2'].nodeValue);
    });
    a(".submit").click(() => {
        let data = {};
        data.type = "addNewGood";
        data.goodType = $(".selected").val();
        data.name = a(".gd-name").text().toLowerCase();
        data.about = a(".gd-about").text().toLowerCase();
        data.cost = a(".gd-cost").text().toLowerCase();
        if (data.goodType === "parfumes") {
            let amount = {};
            let count = {};
            for (var i = 0; i < $(".gd-parfume-amount").length; i++) {
                amount[i] = a(`.gd-parfume-amount:nth-child(${i+1})`).text();
            }
            for (var i = 0; i < $(".gd-parfume-count").length; i++) {
                count[i] = a(`.gd-parfume-count:nth-child(${i+1})`).text();
            }
            data.amount = amount;
            data.count = count;
        } else if (data.goodType === "dresses") {
            let amount = {};
            let count = {};
            for (var i = 0; i < $(".gd-sc-amount").length; i++) {
                amount[i] = $('.gd-sc-amount')[i].innerText;
            }
            for (var i = 0; i < $(".gd-sc-count").length; i++) {
                count[i] = $('.gd-sc-count')[i].innerText;
            }
            data.amount = amount;
            data.count = count;
            data.color = a(".good-colors select")['0'].selectedOptions['0'].attributes['2'].nodeValue;
        }
        data.category = a(".good-category select")['0'].selectedOptions['0'].attributes['1'].nodeValue;
        if (typeof $(".file").attr('datasrc')!=="undefined") {
            data.image = $(".file").attr('datasrc');
        } else {
            $(".file").click();
        }

        req.data = data;
        req.success = e => {
            // console.clear();
            a("._404NotFound").removeClass('_404NotFound');
            e = e.split("---");
            if (e['0'] === "error") {
                $(`.${e['1']}`).addClass('_404NotFound').focus();
                alert(e['2']);
            } else if (e['0'] === 'done') {
                alert("Mal Uğurla daxil edildi");
                window.location.reload();
            } else {
                console.log(e.join('---'));
            }
        }
        a.ajax(req);
    });
})(function(a){return $||jQuery}(!1));

这是我的PHP

<?php

    // Nedded libraries

    require("../../const.php");
    require("md6.php");

    // Main variables
    $h = new md6();

    // Defines

    define("p", $_POST);
    define("t", p['type']);

    // Functions

    function c($t, $p = false||0) {
        if ($p === true || $p === 1) {
            if (strlen($t) !== 64) return false;
            return true;
        }
        if (strlen($t) < 4) return false;
        if (strlen($t) > 32) return false;
        return true;
    }

    function d($arr) {
        for ($i = 0; $i < count($arr); $i++) {
            if (empty($arr[$i])) return false;
            if (!is_numeric($arr[$i])) return false;
        }
        return true;
    }

    function b($t) {
        if ($t === "") return false;
        return true;
    }

    function i($t) {
        if ($t === "") return false;
        $t = explode(";", $t);
        for ($i = 0; $i < count($t); $i++) {
            $p = explode(".", $t);
            if ($p!="png"&&$p!="jpg"&&$p!="jpeg"&&$p!="mp4") return false;
            if (!file_exists(IMG.$t)) return false;
        }
        return true;
    }

    // newGood Page

    if (t === "loadImage") {
        $n = "c/".$h->hex(time().mt_rand(000,999), 64, mt_rand(000,999), 16).".".explode("/", $_FILES['image']['type'])['1'];
        rename($_FILES['image']['tmp_name'], IMG.$n);

        echo $n;
    } elseif (t === "addNewGood") {
        $gd = p['goodType'];
        $nm = p['name'];
        $ab = p['about'];
        $cs = p['cost'];
        $am = p['amount'];
        $ca = p['category'];
        $ct = p['count'];
        $cl = p['color'];
        $im = p['image'];


        if (!c($nm)) {
            $error[] = "gd-name---Ad daxil edilməyib və ya uzunluğu [4-32] standartına uyğun gəlmir.";
        }
        if (!d($am)) {
            $error[] = "gd-sc-amount---Daxil etdiyiniz say ya boşdur ya da qadağan olunmuş simvollardan ([a-z][A-Z][!@#$%^&*(){}[],<>?\\|/]) istifadə edilib.";
        }
        if (!d($ct)) {
            $error[] = "gd-sc-count---Daxil etdiyiniz say ya boşdur ya da qadağan olunmuş simvollardan ([a-z][A-Z][!@#$%^&*(){}[],<>?\\|/]) istifadə edilib.";
        }
        if (!c($cs)&&!is_numeric($cs)) {
            $error[] = "gd-cost---Qiymət daxil edilməyib və ya da qadağan olunmuş simvollardan ([a-z][A-Z][!@#$%^&*(){}[],<>?\\|/]) istifadə edilib.";
        }
        if (!b($ca)) {
            $error[] = "good-category select---Kateqoriya seçilməyib.";
        }
        if ($gd === "dresses") {
            if (!b($cl)) {
                $error[] = "good-colors select---Kateqoriya seçilməyib.";
            }
        }
        if (!i($im)) {
            $error[] = "good-file---Seçilmiş medianın tipi standarta [jpg,jpeg,png,mp4] uyğun deyil.";
        }

        if (!empty($error))
            echo "error---" . $error['0'];
        else
            "done_";
    }
    print_r(p);

那是我的HTML

    <?php

        $x = true;

        $a = array ('black', 'red', 'green', 'blue', 'white');

        ?>
            <form enctype="multipart/form-data" method="post"><!-- #973692 -->
                <section class="good-data good-type data-all hidden">
                    <input required type="hidden" class="page" value="<?=$_REQUEST['selectedPage']?>" />
                    <input required type="hidden" class="sec" value="<?=time();?>" />
                    <input required type="hidden" class="date" value="<?=date("Y-m-d H:i:s", time());?>" />
                    <input required type="hidden" class="selected" value="" />
                </section>
                <h1>yeni mal &#601;lav&#601; et</h1>
                <section class="good-data good-type">
                    <label>
                        <input type="radio" class="data-area parfume" dataopenclassnames="data-parfumes;data-all" name="type" datatype="parfumes" onchange="areaFinder(this)" />
                        <b class="bgtext" data="Ətir"></b>
                    </label>
                    <label>
                        <input type="radio" class="data-area dress" dataopenclassnames="data-dresses;data-all" name="type" datatype="dresses" onchange="areaFinder(this)" />
                        <b class="bgtext" data="Paltar"></b>
                    </label>
                    <!-- <input type="radio" class="" name="type" /> -->
                </section>
                <section class="good-data good-name data-all hidden">
                    <h3>Mal&#305;n ad&#305; <i class="fa fa-star"></i></h3>
                    <div contenteditable="plaintext-only" class="gd-name"></div>
                </section>
                <section class="good-data good-about data-all hidden">
                    <h3>Mal haqq&#305;nda</h3>
                    <div contenteditable="plaintext-only" class="gd-about"></div>
                </section>
                <section class="good-data good-sc data-dresses hidden">
                    <h3>&#214;l&#231;&#252; v&#601; miqdar&#305; <i class="fa fa-star"></i></h3>
                    <div colspan="2" class="afl"><span contenteditable="plaintext-only" class="gd-sc-amount"></span><span>=></span><span contenteditable="plaintext-only" class="gd-sc-count"></span></div>
                    <button type="button" onclick="$(this).before('<div colspan=\'2\' class=\'afl\'><span contenteditable=\'plaintext-only\' class=\'gd-sc-amount\'></span><span>=></span><span contenteditable=\'plaintext-only\' class=\'gd-sc-count\'></span></div>')" class="fa fa-plus gd-sc-add"></button>
                </section>
                <section class="good-data good-parfume data-parfumes hidden">
                    <h3>Miqdar v&#601; say&#305; <i class="fa fa-star"></i></h3>
                    <div colspan="2" class="afl"><span contenteditable="plaintext-only" class="gd-parfume-amount"></span><span>=></span><span contenteditable="plaintext-only" class="gd-parfume-count"></span></div>
                    <button type="button" onclick="$(this).before('<div colspan=\'2\' class=\'afl\'><span contenteditable=\'plaintext-only\' class=\'gd-parfume-amount\'></span><span>=></span><span contenteditable=\'plaintext-only\' class=\'gd-parfume-count\'></span></div>')" class="fa fa-plus gd-parfume-add"></button>
                </section>
                <section class="good-data good-cost data-all hidden">
                    <h3>Mal&#305;n qiym&#601;ti <i class="fa fa-star"></i></h3>
                    <div contenteditable="plaintext-only" class="gd-cost"></div>
                </section>
                <section class="good-data good-category data-all hidden">
                    <select>
                        <option class="data-all" disabled selected value="r2124">Birini se&#231;in</option>
                        <option class="data-parfumes" value="p_w">Qad&#305;n &#601;tir</option>
                        <option class="data-parfumes" value="p_m">Ki&#351;i &#399;tir</option>
                        <option class="data-dresses" value="d_w">Qad&#305;n geyimi</option>
                        <option class="data-dresses" value="d_m">Ki&#351;i geyimi</option>
                    </select>
                </section>
                <section class="good-data good-colors data-dresses hidden">
                    <select>
                        <option class="data-all" disabled selected>Birini se&#231;in</option>
<?php

for ($i = 0; $i < count($a); $i++) {
    echo "                      <option class=\"data-dresses\" style=\"background-color: {$a[$i]};\" datacolor=\"{$a[$i]}\"></option>";
}

?>
                    </select>
                </section>
                <section class="good-data good-file data-all hidden">
                    <label class="inputfile">
                        <input required type="file" class="file" multiple />
                        <!-- Show and can be cut images -->
                        <i class="fa fa-upload"></i>
                        <span class="spanText">&#350;&#601;kill&#601;ri se&#231;in (1 - 10) aras&#305; <i class="fa fa-star"></i></span>
                    </label>
                    <div class="showImages"></div>
                    <div class="dropImages"></div>
                </section>
                <section class="submit data-all hidden">
                    <input required type="button" class="button" value="G&#246;nd&#601;r" />
                </section>
            </form>
            <script src="/script/ajax.jquery.min.js"></script>
            <script src="/script/p.newGood.js"></script>

我不明白为什么它返回空数组。在这里,我制作数据和req作为对象,因为当我在此处最小化代码时,最小代码是可读的。请求变量(对象)是我发送到Web服务器的请求。数据变量(对象)是我发送到后端的请求的内容。 预先安排。

1 个答案:

答案 0 :(得分:0)

我发现了错误。我将请求开始时间设置为1次,但发送次数超过1次;