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

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

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

我在 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
}

答案1

尝试更改以下行:

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);

注意真的论据异步参数。您的调用的同步特性很有可能导致重大问题。

相关内容