开始时,找了很多资料,都调用不成功,后来,把IP换成:127.0.0.1后,成功;再换成真实的IP: 149, 也调用 成功了;郁闷,到底是错误在哪里?
<script type="text/javascript" src="../../Scripts/jquery-1.8.3.js"></script>
<script type="text/javascript">
//提交订单.
$(".wcfInvoke").click(function () {
alert("classIdData:");
//var actionUrl = 'shttp://127.0.0.1/Service1.svc/GetData?jsoncallback=?';
var actionUrl = 'http://192.168.5.149/Service1.svc/GetData?jsoncallback=?';
$.ajax({
type: 'GET',
url: actionUrl,
data: { value: 3 },
dataType: "json",
contentType: 'application/json',
success: function (result) {
alert("成功:" + result);
var data = eval('(' + result + ')');
if (String(data.d).length > 0) {
alert(data.d);
}
else {
alert("服务器超时");
}
},
error: function (err) {
alert("1111");
alert(err);
},
complete: function () {
}
});
});
web.config文件,备注一下:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="serviceMetadataBehavior">
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
<serviceMetadata httpGetEnabled="true" />
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true">
<baseAddressPrefixFilters>
<add prefix="string" />
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
<services>
<service name="WcfService2.Service1" behaviorConfiguration="serviceMetadataBehavior">
<endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="WcfService2.IService1" bindingConfiguration="webBinding">
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="webBinding" crossDomainScriptAccessEnabled="true"></binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" />
</system.webServer>
<appSettings>
<add key="Access-Control-Allow-Origin" value="*" />
</appSettings>
IIS, 网站->HTTP响应标头:名称:Access-Control-Allow-Origin, 值:*