W
W
WebforSelf2020-02-29 18:34:16
JavaScript
WebforSelf, 2020-02-29 18:34:16

Why doesn't the form close after submitting?

<spoiler title="">
    (function($){
        'use strict';
        
        var name, id, img;
        
        $('.js-one-click').on('click', function(e){
            e.preventDefault();
            
            var $btn = $(this);
            
            name = $btn.data('name');
            id = $btn.data('id');
            img = $btn.data('img');
                
            $('#oneclick-product').text(name);
            $('#oneclick-image').css('background-image', 'url(' + img + ')');

          
 $.fancybox.open({
  src  : '#oneclick',
  type : 'inline'
});
      
        });
        
        $('#oneclick').on('submit', function(e){
            e.preventDefault();
 
            $.ajax({
                type: "post",
                url: "ajax/oneclick.php",
                data: {
                    'amount': 1, 
                    'variant': id,
                    'name': $('#oneclick-name').val(),
                    'phone': $('#oneclick-phone').val() 
                },
                dataType: 'json',
                success: function(data){
                $.fancybox.close();
                }
            });
        });
        
    })(window.jQuery);
</spoiler>


There is a code, in fact, what it does, when the button is clicked, it collects the date attributes and opens a modal window.
When the send button is pressed, it sends the data and should close the pop-up window (ideally, of course, in the pop-up window, display the text that the form has been accepted and then after 2-3 seconds turn off) is in principle implementable on jquery, but now it doesn’t even close when sending. Where did you go wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
yarnstart, 2020-02-29
@WebforSelf

For those who got here:
You tell jquery that the server will definitely return json to you, and it returns you an empty string.
Execute the code
JSON.parse('');
It will give an error, the jakver executes this code for you so that you don't have to write it with your hands JSON.parse(data)every time, but because this script crashes with an error, an error callback is triggered
In the code that you saw on another site, the real json may be returned from the server, or there was a different version of jquery (potentially possible)
If you are not sure that the server will return a certain data format or return them at all, just don't specify dataType.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question