AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 560267
Accepted
Nymeria
Nymeria
Asked: 2014-12-12 08:28:07 +0800 CST2014-12-12 08:28:07 +0800 CST 2014-12-12 08:28:07 +0800 CST

为什么 XMLHttpRequest 在 readyState 属性中只发送“已建立的服务器连接”?

  • 772

我尝试将 JSON web 服务的结果存储到列表视图中。

我在 javascript 中创建了我的请求,但我的readyState价值永远1不会改变。我不明白哪里出了问题。

这是我的输出:

就绪状态更改为:1

   import QtQuick 2.0
import Ubuntu.Components 1.1
import Ubuntu.Components.ListItems 0.1 as ListItem
/*!
    \brief MainView with a Label and Button elements.
*/

MainView {
    // objectName for functional testing purposes (autopilot-qt5)
    objectName: "mainView"

    // Note! applicationName needs to match the "name" field of the click manifest
    applicationName: "com.ubuntu.developer.username.gms"

    /*
     This property enables the application to change orientation
     when the device is rotated. The default is false.
    */
    //automaticOrientation: true

    // Removes the old toolbar and enables new features of the new header.
    useDeprecatedToolbar: false

    width: units.gu(100)
    height: units.gu(75)

    PageStack {
        id: pageStack
        Component.onCompleted: push(page0)

        Page {
            id: page0
            title: i18n.tr("Index")
            visible: false

            Column {
                anchors.fill: parent
                ListItem.Standard {
                    text: i18n.tr("View history")
                    onClicked: pageStack.push(page1)
                    progression: true
                }
                ListItem.Standard {
                    text: i18n.tr("External page")
                    onClicked: pageStack.push(Qt.resolvedUrl("MyCustomPage.qml"))
                    progression: true
                }
            }
        }
        Page {
            title: "Rectangle"
            id: page1
            visible: false

            Rectangle {
                width: 320
                height: 480
                ListView {
                    id: view
                    anchors.fill: parent
                    delegate: Text {
                        anchors.fill: parent
                        width: view.width
                        Text { text: "ttile: " + modelData.title }
                        Text { text: "iconeSource: $" + modelData.media.m }
                    }
                    function request() {
                        var xhr = new XMLHttpRequest();
                        xhr.onreadystatechange = function() {
                            console.log("Ready state changed to : " + xhr.readyState +" \n");
                            if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
                                console.log('HEADERS_RECEIVED')
                            } else if(xhr.readyState === XMLHttpRequest.DONE) {
                                console.log('DONE')
                                var json = JSON.parse(xhr.responseText.toString())
                                view.model = json.items
                            }
                        }
                        xhr.open("GET", "http://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1&tags=munich");
                        xhr.send();
                    }
                    Component.onCompleted: {
                        request();
                    }
                } // listView
            } // rectangle
        } // page
    }

}

通常我应该多次进入 onreadystatechange 函数......

PS:我在 app.apparmor 中添加了“网络”权限

{
    "policy_groups": [
        "networking"
    ],
    "policy_version": 1.2
}
application-development
  • 1 1 个回答
  • 638 Views

1 个回答

  • Voted
  1. Best Answer
    Aaron Hastings
    2014-12-12T09:12:33+08:002014-12-12T09:12:33+08:00

    尝试更改以下行:

    xhr.open("GET", "http://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1&tags=munich");
    

    ...至:

    xhr.open("GET", "http://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1&tags=munich", true);
    

    请注意提供给异步参数的true参数。您的呼叫的同步性质很有可能导致重大问题。

    • 1

相关问题

  • 应用程序通常在哪里存储数据?

  • 我应该为 Vala 使用哪个 IDE?

  • 安装用户应用程序时,“最佳实践”建议它们位于何处?

  • 为 Python 应用程序设置构建系统

  • 如何在 Ubuntu 上开发 .NET 应用程序?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve