Cordova:如何诊断ajax无法用于UWP(Windows Store)应用程序 [英] Cordova : How to diagnose ajax not working for UWP (windows store) application

查看:68
本文介绍了Cordova:如何诊断ajax无法用于UWP(Windows Store)应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Cordova应用程序,在构建该应用程序并作为Windows UWP应用程序运行时,由于某种原因,ajax调用被阻止了我的工作网络.

I have a simple Cordova application, where when built, and running as a Windows UWP application, has ajax calls are somehow being blocked my work network.

我之前已经问过很多次了,但是由于没有任何解决方案,思想会尝试改写.

I have asked this many times before, but thought would try to reword, as have never got any solutions.

应用程序ajax调用可以在我的家用计算机上正常工作,或者看来是大多数其他网络.当它起作用时,我可以在 Wireshark

The application ajax calls work fine on my home machine, or whats seems to be most other networks. When it works, I can see all the output in Wireshark

在我的工作机上,连接到我们的工作网络时,我在Wireshark中什么都看不到.如果我指向在本地主机上运行的服务器,则该应用程序可以正常工作(但在Wireshark中什么都看不到,但这也许是因为它是本地主机)

When on my work machine, connected to our work Network, I see absolutely nothing in Wireshark. If I point to a server running on localhost, the app does work (but I see nothing in Wireshark, but perhaps this is because it is localhost)

如果我在 UWP容器之外运行该应用程序,即我只是在同一台计算机,同一网络,但通过桌面浏览器上运行(就像您为Cordova进行调试一样),它也可以正常工作.

If I run the app outside of the UWP container, ie I just run on the same machine, on the same network, but via the desktop browser (as you do for Cordova to debugging), it also works fine.

因此,它似乎在到达网络之前就已被阻止,因为我们在Wireshark中根本看不到任何东西,因此这排除了主机无法访问,CORS等问题,因为它甚至没有发送到服务器.

So it appears to be blocked before it even gets to the Network, as we see nothing in Wireshark at all, so this rules out host not reachable, CORS etc as it hasn't even been sent to the server.

我可以通过Visual Studio在调试中运行此代码,然后运行以下测试代码...

I can run this in debug via Visual Studio, and I run the following test code...

document.addEventListener('deviceready', callUrl, false);

function callUrl() {

  console.log('callUrl'); 
  var xhr = new XMLHttpRequest();

  xhr.onreadystatechange = function () {
    var DONE = 4; // readyState 4 means the request is done.
    var OK = 200; // status 200 is a successful return.
    console.log(xhr.readyState);
    if (xhr.readyState === DONE) {
      if (xhr.status === OK)
        console.log(xhr.responseText); // 'This is the returned text.'
    } else {
      console.log('Error: ' + xhr.status); // An error occurred during the request.
    }
  }

  xhr.open('GET', 'https://httpbin.org/get');
  xhr.send(null);         

};

onreadystatechange被调用两次,其中xhr.readyState首先被调用2,然后被调用4. status始终为0.

onreadystatechangeis called twice, with xhr.readyState first 2 and then 4. status is always 0.

我该如何诊断是什么原因阻止了这种情况?我真的一点儿都不知道.这是一个低水平的东西,但是怎么看呢?我也浏览了Windows事件日志,但什么也没找到.

How can I diagnose what is blocking this?? I have absolutely no idea. This is something low level, but how to see what? I have also looked through Windows Event logs, but can find nothing.

Ajax调用仅返回 0 并带有空白说明.我们的网络管理员只是说我的应用存在问题(我尝试了多个Cordova应用,包括基本的测试应用)

The Ajax call just returns 0 with a blank description. Our Network administrator just says there is something wrong with my app (I have tried multiple Cordova apps, including just basic test apps)

在此先感谢您的帮助.

响应@Xavier的评论,我在AppxManifest.xml中的所有内容(是从构建的.appxupload文件中提取的)是

In response to the comment from @Xavier, all I have in my AppxManifest.xml (that I extracted from my built .appxupload file) is

<Capabilities> 
  <Capability Name="internetClient" /> 
</Capabilities> 

有关此功能的一些文档此处,其中包含以下内容(在页面底部).

There is some documentation on the capabilities here, where we have the following (right at the bottom of the page)..

    The following capabilities are unavailable when deploying your Remote Mode application to the Windows Store:

    Enterprise Authentication (enterpriseAuthentication)
    Shared User Certificates (sharedUserCertificates)
    Documents Library (documentsLibrary)
    Music Library (musicLibrary)
    Pictures Library (picturesLibrary)
    Videos Library (videosLibrary)
    Removable Storage (removableStorage)
    Internet client/server (internetClientServer) - note that internetClient is still permitted
    Private network client/server (privateNetworkClientServer)

我不确定我们是否甚至在Visual Studio(config.xml)中进行了设置,但是以上内容似乎表明其中一些不能使用?

I am not sure were we even set these in Visual Studio (config.xml), but also the above seems to be saying some of these can't be used?

internetClient是否足够(我只想作为客户端呼出服务器,而不希望充当服务器).

Should the internetClient be enough (I only want to call out to a server as a client, not act as a server).

此外,这似乎足以在除我的工作之外的大多数网络上工作.

Also, this seems to be enough to work on most Networks except for my Work..

响应CSP的回复...

In response to the reply on the CSP...

此处上进行了讨论.我在Visual Studio中创建的测试Cordova应用程序确实具有以下功能:

There is a discussion on this here. The test Cordova app I created in Visual Studio does have this :

 <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">

令人惊讶的是,我在Ionic index.html中没有看到此消息,但这也许是因为它正在使用白名单插件吗?

Surprisingly, I don't see this in my Ionic index.html, but perhaps this is because it is using the white list plugin?

我的Ionic应用程序在config.xml中具有以下内容

My Ionic application has the following in the config.xml

  <access origin="*" />
  <allow-intent href="http://*/*" />
  <allow-intent href="https://*/*" />
  <allow-intent href="tel:*" />
  <allow-intent href="sms:*" />
  <allow-intent href="mailto:*" />
  <allow-intent href="geo:*" />
  <allow-navigation href="http://localhost:8080/*" />
  ....
  <plugin name="cordova-plugin-whitelist" spec="^1.3.1" />

如果ajax甚至有错误,那就是与CSP有关,那将是很棒的事情,但是我们什么也没得到.

It would be great if the ajax even had some error saying it was something to do with CSP if that is what it is, but we just get nothing.

再一次,很奇怪,该问题仅发生在我的工作网络(通过电缆或WIFI)上.如果我通过另一个连接(例如,将其绑定到我的手机单元)运行确切的机器(例如Surface平板电脑),则所有操作都将按预期进行.因此,这一定是与网络(或可能是防火墙)有关的设置,我也发现这很奇怪,因为至少在Wireshark中我会看到一些东西.

Once again, it is weird that the problem only occurs on my work network (either on cable or our WIFI). If I run the exact machine (e.g. a Surface tablet)over another connection (e.g. tether it to my phones cell), then all works as expected. So it must be some setting to do with the network (or firewall maybe), which also I find strange as surely I would at least then see something in Wireshark.

能够调试"到ajax调用,并查看失败的地方,将是很棒的事情.

Would be great to be able to "debug into" the ajax call, and see where it is failing.

阅读其中一条评论后,我用提琴手看我是否能看到任何东西,我可以这么做...

After reading one of the comments, I used fiddler to see if I could see anything, which I do...

它甚至报告200(不正确),我的请求仍然失败.

It even reports 200 (which is not correct), my request still fails.

推荐答案

根据,您需要请求privateNetworkClientServer功能才能与本地网络通信.

According to this, you need to request the privateNetworkClientServer capability in order to communicate with a local network.

请注意,仅当您的应用程序配置为

Note that this capability only works if you app is configured for local mode (your app will be rejected from the store if using this capability in remote mode).

要启用本地模式,您需要在cordova config.xml中设置<preference name="WindowsDefaultUriPrefix" value="ms-appx://" />.

To enable local mode, you need to set <preference name="WindowsDefaultUriPrefix" value="ms-appx://" /> in your cordova config.xml.

请注意,本地模式可能会导致其他问题,因为您不能使用例如本地模式下的内联脚本(违反CSP)

Note that local mode might lead to other issues, as you cannot use e.g. inline scripts in local mode (CSP violation)

这篇关于Cordova:如何诊断ajax无法用于UWP(Windows Store)应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆