简体   繁体   English

带有QML的JavaScript的怪异行为

[英]Weird behavior of JavaScript with QML

weird behavior of java script. Java脚本的怪异行为。 I am trying to develop a simple example application using QT-QML and JavaScript. 我正在尝试使用QT-QML和JavaScript开发一个简单的示例应用程序。
In this i am using HTTP Requests, I have on Button that sends HTTP request using JavaScript. 在这种情况下,我正在使用HTTP请求,而我使用的是Button,它使用JavaScript发送HTTP请求。

When I receive response of the HTTP request in the call back function i am trying to read the state of the HTTP response as follows. 当我在回调函数中收到HTTP请求的响应时,我尝试按以下方式读取HTTP响应的状态。

if( httpReq.readyState == 4 ) //Problem
{   
    if(httpReq.status == 200 )
    {
           ...

I am trying to check if readyState is 4 (4 represents complete/done) 我正在尝试检查readyState是否为4 (4表示完成/完成)
But if conditions fails to check this and gets evaluated to true regardless of readyState value. 但是,如果条件无法检查到这一点,并且无论readyState值如何,都将其评估为true。
For example, if readyState is 0 (0 == 4) then also if condition gets evaluated to TRUE which should not. 例如,如果readyState0 (0 == 4),那么如果condition的求值结果为TRUE,则不应该这样做。
Why this might be happening. 为什么会发生这种情况。

I have also tried 我也尝试过

 1. if( parseInt(httpReq.readyState) == 4 ) 
 2. if( Number(httpReq.readyState) == 4 )  
 3. if( httpReq.readyState == '4' )  

Above conditions also give the same results and gets evaluated to TRUE regardless of readyState value. 上面的条件也给出了相同的结果,并且无论readyState值如何,都将其评估为TRUE。
Is there any problem with my JavaScript Interpreter. 我的JavaScript解释器有什么问题吗?

Thanks. 谢谢。

------UPDATE----- ------更新-----

Thing is that, I have QML application (which sends HTTP request) and HTTP server (Which servers this QML HTPP request) both in the same application/process. 问题是,我在同一应用程序/进程中都有QML应用程序(发送HTTP请求)和HTTP服务器(该QML HTPP请求中的哪个服务器)。 When I Separate HTTP server and QML application in two different application/executable it does work, and when i combine both the applications in same executable then it creates problem. 当我在两个不同的应用程序/可执行文件中分离HTTP服务器和QML应用程序时,它确实可以工作,而当我将两个应用程序合并在同一个可执行文件中时,则会产生问题。 When i combine both HTTP server and QML application in one executable QML JavaScript interpreter starts behaving weird. 当我将HTTP服务器和QML应用程序合并到一个可执行的QML JavaScript解释器中时,它开始变得怪异。 I am running QML application in a Separate Thread before running Web server. 在运行Web服务器之前,我正在单独的线程中运行QML应用程序。

Did you try: 你试过了吗:

if( httpReq.readyState == 4 ) //Problem
{   
  console.log("Evaluated to true with: " + httpReq.readyState);

...

To assert that the condition was truly evaluated to true with a wrong integer? 要断言条件是使用错误的整数真正地评估为true?

Alternatively, since you call this in QML, this might come from the way you use javascript with QML, can you show how you invoke the javascript from the QML? 另外,由于您是在QML中调用此代码的,所以这可能是您在QML中使用javascript的方式,您能否说明如何从QML调用javascript?

A minimal example with the described behaviour would be helpful. 一个描述行为的最小示例将很有帮助。

The following code works for me without problems: 以下代码对我来说没有问题:

import QtQuick 1.0

Item {
    Component.onCompleted: {
        var req = new XMLHttpRequest();
        req.onreadystatechange = function() {
            console.log("readyState: " + req.readyState);

            if (req.readyState == XMLHttpRequest.DONE) { // 4 instead of 'XMLHttpRequest.DONE' works here too
                console.log("Request complete");

                if (req.status == 200) {
                    console.log("Status code: 200");
                    console.log(req.responseText.slice(0, 50) + "...")
                }
            }
        }

        req.open("GET", "http://stackoverflow.com/");
        req.send();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM