【问题标题】:Accessing WCF in Azure by $.ajax yields "NETWORK_ERR: XMLHttpRequest Exception 101"通过 $.ajax 在 Azure 中访问 WCF 会产生“NETWORK_ERR: XMLHttpRequest Exception 101”
【发布时间】:2013-06-19 20:47:53
【问题描述】:

我尝试过同步和异步方法。我已经测试了许多线程中建议的所有不同的东西,但我仍然不断收到上述错误。

对于 IE,我得到“No transport”,而在 FF 中,我得到一个可怕的“Exception...“Failure”nsresult:“0x80004005 (NS_ERROR_FAILURE)”。 p>

当我从浏览器中的 URL 行访问该服务时,我会看到返回的文本,表明该服务已启动并正在运行。 (MyCloud.azurewebsites.net/MyService.svc/Ping)

但是当我尝试使用这个 JS 访问它时它不起作用。

$.ajax({
  cache: false,
  async: false, //have tried true too
  type: "GET",
  url: "http://MyCloud.azurewebsites.net/MyService.svc/Ping",
  datatype: "text",
  success: function (response) { ... },
  error: function (xhr, requestStatus, errorText) { ... }
});

我测试了不同的浏览器(Chrome 除外)。由于 CORS 问题,我测试了声明一个不同的端点,并对安全性进行了一些更改(transport 而不是 none)。我仍然会遇到错误。而寻找那些只会带来更多的困惑。

现在,我已经解决这个问题超过一天了,我决定安排故障排除。

  • 上面的JS完全正确吗?
  • 以下端点定义和服务声明是否也正确?
  • 我还能测试什么?我该如何调查这个问题?
<endpoint name="UrlEndPoint"
          behaviorConfiguration="UrlEndPointBehavior"
          address=""
          binding="webHttpBinding"
          contract="MyNamespace.IMyService"/>

<behavior name="UrlEndPointBehavior">
  <webHttp helpEnabled="true"/>
</behavior>
[OperationContract]
[WebInvoke(UriTemplate = "Ping", Method = "GET")]
String Ping();

此刻我完全迷失了,又病又累。朝着正确的方向前进会很棒。

【问题讨论】:

  • 这是跨域请求吗?
  • @MikeBrant 是的。该服务在 Azure 上,呼叫可以来自任何地方。我读到有些人对 CORS 有问题,但正如我在问题中提到的那样,这让我很头疼。 :)

标签: jquery ajax wcf azure


【解决方案1】:

这是一个跨站点脚本错误。您需要调用服务器端处理程序(代理)来处理对 Azure 环境的请求。因此,您的 AJAX 调用看起来更像这样:

 $.ajax({
   type: "GET",
   url: 'some/local/server-side/url/to/handle/the/call/to/"http://MyCloud.azurewebsites.net/MyService.svc/Ping",
   datatype: "text",
   success: function (response) { // handle response here from server-side proxy here },
   error: function (xhr, requestStatus, errorText) { ... }
 });

问题是由于浏览器中的 XSS(跨站点脚本)限制,您无法调用外部域。这是您选择的浏览器实施的安全预防措施。

【讨论】:

    猜你喜欢
    • 2013-04-01
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 2012-12-11
    • 1970-01-01
    • 2021-10-30
    • 2020-05-23
    相关资源
    最近更新 更多