【发布时间】:2015-07-17 14:30:25
【问题描述】:
嘿嘿,
我知道以前有人问过这个问题,并且我已经阅读了这里每个问题的每一个答案,但似乎仍然无法让我的项目正常工作。不过,我的情况与其他问题有点不同,不确定这是否有什么不同,但我们开始吧。
我有一个由 Web 服务组成的项目。仅服务就可以正常工作,这没有问题。还有另一个项目处理 UI 部分并通过 jquery ajax 调用这些服务。它一直运行良好,因为我之前没有在 Google Chrome 上测试过这个案例。一切都可以在 Internet Explorer 上完美运行。
现在;我的 Web 服务应用程序在端口 40000 (localhost:40000) 上运行,UI 项目在其他一些随机端口上运行,但仍在 localhost 中。就像我说的,我的 ajax 调用等在 Internet Explorer 上运行良好,但在 Google Chrome 上却失败了。控制台显示以下错误:
XMLHttpRequest cannot load http://127.0.0.1:40000/abc.asmx/TheMethodName. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:17256' is therefore not allowed access. The response had HTTP status code 400.
顺便说一句,之前好像是“localhost:40000”,所以网上有帖子建议我应该把它改成IP地址而不是localhost。
无论如何,我还在我的网络服务项目中编辑了我的 web.config 文件并添加了以下内容(这没有任何意义)
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="crossDomain" crossDomainScriptAccessEnabled="true">
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:17256" />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
<add name="Access-Control-Max-Age" value="1728000" />
</customHeaders>
</httpProtocol>
</system.webServer>
顺便说一句,如果我使用第一行:它完全失败并说响应头消息不匹配,我什至无法启动服务调用。
是的,我还编辑了我的 ajax 调用,添加了以下参数:
crossDomain: true,
现在,我在这里缺少什么?我已经为此工作了好几天,我快要失去理智了:) 改变项目的整个结构(服务 - 我的意思是 UI 样式)有点太晚了,因为已经将大量代码写入服务和用户界面项目。请帮助我很绝望! :)
干杯
编辑:
所以事实证明我需要使用 JSONP,而不是 JSON——因为它不会被访问控制所困,这很好。我可以将我的 ajax 调用转换为 JSONP 调用。但我需要另一个帮助:)
我已经编写了一个全局 ajax 方法,该方法从单独页面上的每个操作中调用,它的工作原理就像魅力一样(当然又是在 Internet Explorer 上)。现在我应该怎么做才能将此包装函数转换为 jsonp 东西?
function RemoteCall(WebService, RemoteMethod, RemoteParameters, callbackResult) {
if (RemoteParameters == null || RemoteParameters == "") {
RemoteParameters = "{ }";
}
var remoteCall = $.ajax({
type: "POST",
url: ProvideService(WebService, RemoteMethod),
data: RemoteParameters,
contentType: ajaxContentType,
dataType: ajaxDataType,
async: true,
crossDomain: true,
success: function (msg) {
msg.header("Access-Control-Allow-Origin", "*");
callbackResult(msg.d);
},
error: function (xhr, ajaxOptions, thrownError) {
callbackResult(xhr.status + '\r\n' + thrownError + '\r\n' + xhr.responseText);
}
});
}
我看到一些帖子,我应该添加一个属性为 jsonp: 'jsonp_callback' 和回调方法(这是我感到困惑的地方)。这个呢?
另一件事;我一直在将我的 json 参数作为文本发送到像
这样的变量中var jSONParams = '{ UserID: "1", EmailAddress: "a@b.com" }';
我应该继续以相同的格式发送它还是应该将其转换为某种 JSON 对象或其他东西?
顺便说一句:我的全局 ajax 函数位于一个完全独立的 JS 文件中 - 但不确定它是否有任何区别......
干杯
【问题讨论】:
-
端点是否使用任何类型的身份验证? Windows 身份验证等?
标签: c# jquery asp.net ajax web-services