P
P
Pazzetif2018-06-21 13:10:38
Google Chrome
Pazzetif, 2018-06-21 13:10:38

Ajax request works in Mozilla, why doesn't it work in Chrome?

I use this https://github.com/nk-o/ajax-contact-form

$('.pop-up-one').on('submit', function(e) {
    e.preventDefault();
        var $form = $(this);

        $.ajax({
            type: 'POST',
            url: $form.attr('action'),
            data: $form.serialize(),
            success: function (response) {
                response = JSON.parse(response);
                if (response.type && response.type === 'success') {

                    $form[0].reset();
                } else {

                }
            },
            error: function (response) {

            }
        });

});


<form class="pop-up-one" action="ajax-contact-form.php">
            <input type="text" placeholder="Введите ваше имя" name="name" required>
            <input type="text" placeholder="Введите ваш телефон" name="message">
            <input type="text" placeholder="E-mail" name="email" required>
            <button type="submit" class="button-pop-up">Отправить</button>
        </form>

5b2b369d1ca25500111812.gif

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Victor P., 2018-06-21
@Pazzetif

It is possible (but not certain) that the matter is in the Content-Type of the Request. Or .serialize() doesn't work correctly. Now let's figure it out.
Try to pass a regular object, you can add a parser function, for example:

//serialize data function
function objectifyForm(formArray) {

  var returnArray = {};
  for (var i = 0; i < formArray.length; i++){
    returnArray[formArray[i]['name']] = formArray[i]['value'];
  }
  return returnArray;
}

Well, and, accordingly, change the data from the data field :
$.ajax({
            type: 'POST',
            url: $form.attr('action'),
            data: objectifyForm($form.serialize()),
            success: function (response) {

Does this work in both browsers?

S
Semyon Beloglazov, 2018-06-21
@Batlab

So where does he not work?
The form handler does not work, namely ajax-contact-form.php, not ajax.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question