将HTML表单输入存入JS对象时遇语法错误求解决(.NET MVC5)
Fixing the "Expected : Comma expected" Error & Implementing the AJAX Submit
First, let's break down why you're seeing that syntax error:
You've placed raw assignment statements directly inside the configuration object passed to $("#newAuction").validate(). Object literals (like the one you're passing to validate()) can only contain key-value pairs (e.g., submitHandler: function() { ... }), not standalone code like vm.auction.Title = .... That's what's causing the parser to throw a "comma expected" error—it's expecting another key-value pair, not arbitrary code.
Here's the corrected implementation that fixes the error and meets your requirements:
Corrected JavaScript Code
<script> $(document).ready(function() { // Initialize your view model var vm = { auction: {}, productIds: [] }; // Set up form validation var validator = $("#newAuction").validate({ submitHandler: function(form) { // Capture form values into the view model (do this on submit, not initialization) vm.auction.Title = $("#title").val(); vm.auction.MinBid = parseFloat($("#minbid").val()); // Parse to numeric type vm.auction.EDate = $("#edate").val(); vm.productIds = [1, 2, 3]; // Replace with dynamic logic if needed // Submit via AJAX with proper JSON formatting $.ajax({ url: "/api/newAuction", method: "POST", contentType: "application/json", // Notify API we're sending JSON data: JSON.stringify(vm) // Convert object to valid JSON string }) .done(function() { toastr.success("Auction Added to the db"); // Reset the view model (modify existing object, avoid new variable scope) vm.auction = {}; vm.productIds = []; // Reset the form to clear inputs validator.resetForm(); }) .fail(function() { toastr.error("Something went wrong"); }); return false; // Prevent default form submission behavior } }); }); </script>
Key Fixes & Improvements:
- Resolved syntax error: All value assignment logic is now inside the
submitHandlerfunction, not directly in the validator's configuration object. - Captures latest input values: Values are collected when the user submits the form, not on page load (which would have grabbed empty initial values).
- Proper JSON serialization: Added
JSON.stringify(vm)andcontentType: "application/json"to ensure your API receives the exact JSON structure it expects (most APIs require this instead of form-encoded data). - Numeric type handling: Converted
MinBidfrom a string to a float so the API gets a numeric value instead of text. - Fixed view model reset: Instead of declaring a new local
vmvariable in the success callback, we reset the properties of the existingvmobject to avoid scope confusion.
Notes:
- If your
productIdsneed to be populated dynamically (e.g., from selected checkboxes), replace the static[1,2,3]with logic like:vm.productIds = $(".product-checkbox:checked").map(function() { return parseInt($(this).val()); }).get(); - Ensure your API endpoint
/api/newAuctionis configured to accept POST requests with the JSON structure you're sending.
内容的提问来源于stack exchange,提问作者hkjhadj1
相关产品推荐
相关产品推荐

