سؤال

Hi I have the following Js code.

The problem is somehow the form is being submitted twice, however, only one instance of it being submitted is showing up on the safari inspector showing the requests....

And it's definitely not an issue with the server side request as I have tested that to check. so It's definitely something client-side.

$('#replyForm').submit(function(e){

    e.preventDefault();

    var userID = $('#replyForm #userID').val();
    var content = $('#replyForm #content').val();

    if(content != '')
    {

        var ajaxURL = BASE_URL + 'ajax/sendMessage/';

        $.ajax({
            type: 'POST',
            url: ajaxURL,
            dataType: 'json',
            data: { 
                userID: userID,
                content: content
            },
            success: function(){

                updateConversation();

            }
        });

        $('#replyForm #content').val('');

    }
    else
    {

        alert('Cannot send an empty message');

    }

    return false;

});
هل كانت مفيدة؟

المحلول

Ok After doing a lot of testing from every angle, I have found out it was server side code doing this! not running the request.

I was using curl to form my requests to the API. and I was running curl_exec twice, once to check on the success and then again to get the result of the call.

This is what was causing the duplication.

Hope this helps some who are having similar issues.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top