简体   繁体   English

尝试从Windows 8应用中的服务器获取数据

[英]Trying to fetch data from server in windows 8 app

I'm unable to use web services as im developing win8 app in javascript. 我无法将网络服务用作使用JavaScript开发win8应用程序。 so i want to fetch data from test.php from div="class". 所以我想从div =“ class”中从test.php获取数据。

Code: 码:

WinJS.UI.Pages.define("xhr.html", {
    ready: function (element, options) {

        document.querySelector("#btn1").onclick = function (e) {
            WinJS.xhr({ url: "http://127.0.0.1:8081/test.php?serial=13", responseType: "document" }).then(function (xhr) {

                document.querySelector(".xhr #results1").innerText = xhr.response.querySelector("div[class='garaz']")[2].src;
                //document.querySelector(".xhr #results1").appendChild(imga);
            });
        };

Error: Exception is about to be caught by JavaScript library code at line 10, column 21 in ms-appx://694ccf96-9429-4c5a-95ec-bde492c2041a/xhr.js 错误: ms-appx://694ccf96-9429-4c5a-95ec-bde492c2041a/xhr.js的第10行第21列的JavaScript库代码将捕获异常

0x800a138f - JavaScript runtime error: Unable to get property 'src' of undefined or null reference 0x800a138f-JavaScript运行时错误:无法获取未定义或空引用的属性“ src”

If there is a handler for this exception, the program may be safely continued. 如果有用于此异常的处理程序,则可以安全地继续执行该程序。


if my question is stupid please ignore it because im just learning and if there is an easy way kindly help me do the same thing. 如果我的问题很愚蠢,请忽略它,因为我只是在学习,并且有一种简单的方法可以帮助我做同样的事情。 its been a month im stuck at this problem. 我已经陷在这个问题上一个月了。 help 救命

If you have many .garaz elements in the response, then use querySelectorAll . 如果响应中有许多.garaz元素,请使用querySelectorAll And also you can make the selector more simple: response.querySelectorAll('.garaz')[2].src 而且,您还可以使选择器更简单: response.querySelectorAll('.garaz')[2].src

The problem was in query selector section i was using src attribute which is attribute of img tag and was using array . 问题出在查询选择器部分,我正在使用src属性,它是img标签的属性,并且正在使用array。 Thanks to https://stackoverflow.com/users/2436109/tenbits 感谢https://stackoverflow.com/users/2436109/tenbits

document.querySelector(".xhr #results1").innerText = xhr.response.querySelector('.garaz').innerText;

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

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