After filling out the form and sending the request, I get an error.

$("#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> 

Error in the console. What could be the problem ?

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

  • in the snippet, the error is not reproduced. Add more details. - Grundy

1 answer 1

Replace the description of the companyHandlerAJAXPostingData function and the purpose of the click handler.

 // Сначала function companyHandlerAJAXPostingData(data) { // ... } // Потом $("#companyButton").click(function () { // ... companyHandlerAJAXPostingData(createCoupon); }); 
  • function declarations pop up to the beginning of the script block or function where the ad is located. - Grundy