Welcome Guest, Not a member yet? Register   Sign In
function being called twice
#1

(This post was last modified: 12-14-2018, 01:22 AM by richb201.)

This is a strange issue. I have a chrome extension (written in javascript and running in the browser) that send XMLHttpRequest over to my CI program. There is a controller on my server called Subit_backend and it has a function called logger_survey (/Subit_backend/logger_survey). This works fine, the logger_survey() runs on the server and it sends back a message to the calling javascript. Works fine. Then immediately logger_survey() is firing again. I have a debugger set at the first line of logger_survey so I can see it is being called again. 

I am pretty sure that my javascript code is not running the send twice. (I also have a debugger on this).
Code:
    var xhr = new XMLHttpRequest();
    xhr.onerror = function() { alert('error'); };
    xhr.open('POST', url, true);
   xhr.setRequestHeader("Content-type", "application/json")
   xhr.setRequestHeader("X-Requested-With",'xmlhttprequest');
    xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
    
   xhr.onload = function (){
        if((xhr.status === 200)&&(xhr.readyState===4)) {
                var jsonResponse2 = JSON.parse(xhr.responseText);
                sendResponse({task: jsonResponse2});
               return true; //The sendResponse callback is only valid if used synchronously, or if the event
handler returns true to indicate that it will respond asynchronously.     
            }
    };
    
    xhr.send(json);
    }
Is there anything I can do to flush the input buffer in CI so I don't get the same request twice? Or can I perhaps check some id and just ignore the immediate, second call?  Anyone seen this?
proof that an old dog can learn new tricks
Reply
#2

(This post was last modified: 12-14-2018, 08:37 PM by richb201.)

Well, I ran a packet sniffer while testing. It seems that the browser IS sending the request twice. I have no idea of why this is and I am fairly certain that it is not running my xhr.send(json) code twice. Here is the packets from the sniffer.

   

BTW, I do get two 200 responses. How can I tell where in my javascript code (which is in a browser extension) the  2nd send is being initiated?
proof that an old dog can learn new tricks
Reply
#3

Show your event handler. Is it just a click event?
Reply
#4

(This post was last modified: 12-15-2018, 11:14 AM by richb201.)

(12-15-2018, 10:00 AM)skunkbad Wrote: Show your event handler. Is it just a click event?
Hey Brian.
Code:
   $('#btnSubmitSurvey').button().click(function () {
        if (iCertified==0)
        {
            alert("you must certify this survey");
            return;
        }
        iCount=0;
        iPercent=0;
        iTotal=0;
        for (iCount=0;iCount<6;iCount++)
        {
            iPercent=$('#tblAppendGridSurvey').appendGrid('getCtrlValue', 'Perc_time',iCount);
            iPercent=parseInt(iPercent);
            iTotal=iPercent+iTotal;
        }
        if (iTotal>100)
                {
            alert("Tasks are be greater than 100%");
            return;
        }
        iCount=0;
        iPercent=0;
        iTotal=0;        
        for (iCount=0;iCount<6;iCount++)
        {
            iPercent=$('#tblAppendGridBCSurvey2').appendGrid('getCtrlValue', 'Perc_time2',iCount);
            iPercent=parseInt(iPercent);
            iTotal=iPercent+iTotal;
        }
        if (iTotal>100)
                {
            alert("Bus Comps are greater than 100%");
            return;
        }
        data=new Array();
        dataStructure=new Array();
        
        data=$('#tblAppendGridSurvey').appendGrid('getAllValue', true);    //get all the values of the grid    
            
        dataStructure.push(data);
        
        data=$('#tblAppendGridBCSurvey2').appendGrid('getAllValue', true);    //get all the values of the grid
        dataStructure.push(data);
        var year = document.getElementById('year').value;
        
        dataStructure.push(year);   //the year of this survey


        //popup a message that data is being sent
//        alert("your surveys are being sent to our server.");
        
        chrome.extension.sendMessage({task:"log_results_survey", values: dataStructure}, function(response) {
            //get results and send them
            alert(response.task);
            logged_in_user = response;
        });
   });
I think that this is what you mean. BTW, I also have this 
Code:
//this stops the dblclick error    
$("*").dblclick(function(event){
    event.stopPropagation();
  });
So the click sends a a "log_results_survey" message, which get heard by the Listener in the background task. The background task runs this:
Code:
     if (request.task =="log_results_survey"){
              send_to_logger_survey(request, sender, sendResponse);
             };
The send_to_logger_survey() appears in my first post. One strange thing though that I just noticed. The response back to the initial popup task comes directly from the callback from the xhr, rather than being sent back from the on-message callback.
proof that an old dog can learn new tricks
Reply
#5

Is '#btnSubmitSurvey'  a <input type='submit' ...> element? If so you probably need to use event.preventDefault()


Code:
$('#btnSubmitSurvey').button().click(function (event) {
   event.preventDefault();
   // the rest of your code    
Reply
#6

(12-15-2018, 11:42 AM)dave friend Wrote: Is '#btnSubmitSurvey'  a <input type='submit' ...> element? If so you probably need to use event.preventDefault()


Code:
<button id="btnSubmitSurvey" type="button">

    Submit Survey </button>

Is this what you are asking?
proof that an old dog can learn new tricks
Reply
#7

(12-15-2018, 11:48 AM)richb201 Wrote:
(12-15-2018, 11:42 AM)dave friend Wrote: Is '#btnSubmitSurvey'  a <input type='submit' ...> element? If so you probably need to use event.preventDefault()


Code:
<button id="btnSubmitSurvey" type="button">

    Submit Survey </button>

Is this what you are asking?

Yes, that answers my question. It is not a type="submit"
Reply
#8

I have not been able to find the answer although it does sound a lot like a bug in Chrome that has been around for a few years. https://productforums.google.com/forum/#...-gfRJLCCjg

Either way, What I'd like to do is to see if I got the same is_ajax_request() within 1 second, just flush the second one. How can I do this easily?
proof that an old dog can learn new tricks
Reply
#9

(This post was last modified: 12-17-2018, 07:00 AM by richb201.)

I appended getTime() value on the the end of the data being sent from the browser. From Wireshark:

from the first POST    value: 1545054382811
from the second POST   value: 1545054382812

Notice that they are just one msec apart. Can I assume that the function send_to_logger_survey(request, sender, sendResponse) is running twice? Here it is again. I doubt that in that one msec an answer came back from the remote server and I replied again.  It almost seems like a javascript "bounce". 

Any ideas on how to diagnose this problem?
proof that an old dog can learn new tricks
Reply
#10

Is there any support for insert ignore? I have this
$this->db->insert('survey_tasks', $data);
and I would like to skip inserting if it already exists.
proof that an old dog can learn new tricks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB