我正在使用带有jsonp端点的ria服务。当我在服务文件中调用我的方法时,它在ie和firefox中工作正常但有时在chrome中工作,有时我得到“经过身份验证的服务不支持跨域javascript回调。”错误。即使我不使用经过身份验证的服务。
这是一段关于我所拥有的代码。
Jsonp服务
[EnableClientAccess(RequiresSecureEndpoint = false)]
public class PPolJsonService : LinqToEntitiesDomainService<PPolAdvEntities>
{
public IQueryable<QuestionEntity> GetCompleteSurvey()
{
............
}
}
javascript代码
function (data) {
var Params = {};
Params.type = 'GET';
Params.url = 'http://127.0.0.1:81/PPolSilverlight-Web-Services-PPolJsonService.svc/JSONP/GetCompleteSurvey;
Params.dataType = 'jsonp';
Params.data = { data:'somedata'};
Params.success = function (data) { };
Params.jsonpCallback = "ppolv2"
$.ajax(Params);
});
在web.config文件中,我的设置为<authentication mode="Forms">
如果我设置<authentication mode="None">
我可以用chrome解决所有问题。但是应用程序的其余部分需要身份验证。这就是为什么我必须将它用作“mode = Forms”。如您所见,我的服务不使用身份验证,
为什么我收到此错误,是否有任何解决方案?
注意:
顺便说一句,我在web.config中有其他设置,比如
<webHttpEndpoint>
<standardEndpoint crossDomainScriptAccessEnabled="true"
automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
或clientaccesspolicy.xml中的这些
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="http://*"/>
<domain uri="https://*" />
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
但他们都没有帮助我。
提前致谢。
答案 0 :(得分:1)
您好试着将此行添加到您的web.config文件中。它支持跨域Ajax请求。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
答案 1 :(得分:0)
我还没有测试,但我想我找到了解决方案。
如果您在应用程序中使用安全端点,并且如果您不需要为jsonp服务使用安全端点,
你可以在
中添加requireSSL =“true” <authentication mode="Forms">
<forms name=".PPolSilverlight_ASPXAUTH" timeout="2880" requireSSL="true" />
</authentication>
使用这一小段代码,您的不安全的jsonp服务将无需身份验证即可运行。