0

После заполнения формы и отправки запроса, я получаю ошибку.

$("#companyButton").click(function () {

        var createCoupon = {

            id:$("#couponid").val(),
            title:$("#couponTitle").val(),
            release_date:$("#couponStartDate").val(),
            expiration_date:$("#couponExpiration").val(),
            amount:$("#amount").val(),
            message:$("#messageOnCoupon").val(),
            price:$("#couponPrice").val(),
            image:$("#couponImage").val(),
            type:$("#couponType").val(),
            formId:$("#coupon-creation").val()

        };

           companyHandlerAJAXPostingData(createCoupon);
    });


    //обший метод для JS файла. 
    function companyHandlerAJAXPostingData(data) {

        var JSONString = JSON.stringify(data);

        console.log(JSONString);

        return;

        $.ajax({

            url:url,
            method:"post",
            data:JSONString,
            contentType:"application/json",
            error:function (message) {

                console.log(JSON.parse(message));

            },
            success:function (data) {

                console.log(JSON.parse(data));

            },
            headers:
            {
                "Accept":"application/json",
                "Accept-Language":"en",
                "Cache-Control":"max-age=3600"
            }
        });
    }
    <form id="CouponCreateHandler">

            <label for="couponId">Coupon ID</label>
            <input placeholder="coupon's id" type="number" id="couponId" minlength="3" maxlength="10" required><br>

            <label for="couponTitle">Coupon Title</label>
            <input placeholder="coupon's title" type="text" id="couponTitle" minlength="5" maxlength="15" required> <br>

            <label for="couponStartDate">Coupon release date</label>
            <input placeholder="coupon's release date YYYY:MM:DD" type="date" id="couponStartDate" required><br>

             <label for="couponExpiration">Coupon expiration date</label>
             <input placeholder="coupon's expiration date YYYY:MM:DD" type="date" id="couponExpiration" required><br>

             <label for="amount">Amount in storage</label>
             <input placeholder="coupon's amount" type="number" id="amount" required><br>

             <label for="messageOnCoupon">Message for coupon</label>
             <input placeholder="coupon's message" type="text" id="messageOnCoupon" required><br>

             <label for="couponPrice">Coupon price</label>
             <input placeholder="coupon's price" type="number" id="couponPrice" required><br>

             <label for="couponImage">Coupon image: .jpg, .bmp, .png, .gif - only</label>
             <input placeholder="coupon's image" type="file" id="couponImage" required> <br>

            <label for="couponType">Coupon type</label>
             <input placeholder="coupon type" type="text" id="couponType" required><br>

            <input type="hidden" id="coupon-creation">

            <input type="button" id="companyButton" value="Create!">

        </form>

  // хедер на html страници! 

<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script src="jquery-3.1.0.js"></script>
    <script src="companyhandler.js"></script>

</head>

Ошибка в консоле. В чём может быть проблема ?

companyhandler.js:125 Uncaught ReferenceError: companyHandlerAJAXPostingData is not defined

1
  • в приведенном сниппете ошибка не воспроизводится. Добавляй больше подробностей.
    – Grundy
    10 ноя 2016 в 16:19

1 ответ 1

1

Местами переставить описание функции companyHandlerAJAXPostingData и назначение обработчика click

// Сначала
function companyHandlerAJAXPostingData(data) {
// ...
}

// Потом
$("#companyButton").click(function () {
// ...

       companyHandlerAJAXPostingData(createCoupon);
});
1
  • объявления функций всплывают к началу блока скрипт либо функции в которой находится объявление.
    – Grundy
    10 ноя 2016 в 16:18

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.