Search code examples
qtjquerypostqtwebkit

Qt Webkit - Browser Interaction issue


I'm developing a Qt program that contains an OpenStreetMap application as a HTML page and this page is able to access a database -via submitting an ajax form that contains the start and end dates of queries- in order to retrieve and visualize queries on the map. I would like to move this querying process to Qt from the HTML/Javascript part. So far I managed to interact with the browser via Qt but I still have a problem that is below:

1) The fetch queries button of Qt is clicked and an alert box is supposed to pop up saying that Ajax POST is failed -the database is not on my current laptop and I should be getting the error when I click either the HTML Browser window's fetch queries button or the Qt's fetch button-

2) But also, whenever I click the Fetch queries button of the HTML Browser, it displays the POST warning but also displays extra POST warning alert boxes depending on how many times I have clicked the Qt's Fetch queries button. -for example if I have clicked the Qt's fetch queries button 5 times in a row and then clicked the HTML window's fetch button once, I get 6 POST failed messages in a row-

The HTML code is like the following:

<form id="ajaxForm" action="index.php" method="post">
Start <input type="text" name = "date1" id = "datepicker" value = "2011-07-13" style = "width:70px">
<input type="text" name = "time1" id = "timepicker1" value = "00:00" style = "width:40px"> 

End <input type="text" name = "date2" id = "datepicker2" value = "2011-07-13" style = "width:70px">
<input type="text" name = "time2" id = "timepicker2" value = "00:01" style = "width:40px">

The post method of AJAX form is this:

<script type="text/javascript">

$(document).ready(function(){

 // ajaxForm submit
 $('#ajaxForm').submit(function() {
  $.ajax({
   type: 'POST',
   url: 'heatQuery.php',
   data: $(this).serialize(),
   dataType: 'json',
   success: function(response)
   {   
       // update the points for heatmap layer          
       updateHeatMap(response);

   },
   error: function(errorMsg)
   {
       alert('Error in Ajax POST');
       }
  });

  return false;
 });
});

</script>

And finally, the Qt code that calls the function is this:

void MainWindow::onButtonClicked() // clicking the button in order to POST 
{
    //the QString a is the same ajax post function as declared above

    QString a = "$(document).ready(function(){$('#ajaxForm').submit(function() {$.ajax({type: 'POST',url: 'heatQuery.php',data: $(this).serialize(),dataType: 'json',success: function(response){updateHeatMap(response);},error: function(errorMsg){alert('Error in Ajax POST');}});return false;});});"; 

    this->view->page()->mainFrame()->evaluateJavaScript(a);

}

Any ideas on what is wrong here? Thanks.


Solution

  • I think I have got the problem. XMLHttpRequest loads your local file successfully, but it returns 0 in request.status, thats why error() gets fired from your jQuery code.

    I ran following example code in QWebView..

    var request = new XMLHttpRequest();  
    request.open('POST', 'file:///C:/hello.txt', true);
    
    request.onreadystatechange = function(e) {  
    
        if(request.readyState == 4)
        {
            // 'responseText' will not be empty if file present..
            alert("response text: " + request.responseText);
            alert("status: " + request.status);
        }
    }
    
    request.send();
    

    Hope this helps..