Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从asp.net中的jQuery调用WCF服务_Wcf_Jquery - Fatal编程技术网

从asp.net中的jQuery调用WCF服务

从asp.net中的jQuery调用WCF服务,wcf,jquery,Wcf,Jquery,我正在尝试在asp.net中使用jQuery调用WCF服务 WCF服务和asp.net网站采用相同的解决方案,但它们运行在不同的端口,如下所示 WCF - http://127.0.0.1:54443/Service1.svc/ asp.net - http://127.0.0.1:57484/WebSite2/ 下面是WCF功能代码 [WebInvoke(Method = "GET",RequestFormat = WebMessageFormat.Json,ResponseFormat

我正在尝试在asp.net中使用jQuery调用WCF服务

WCF服务和asp.net网站采用相同的解决方案,但它们运行在不同的端口,如下所示

 WCF - http://127.0.0.1:54443/Service1.svc/
 asp.net - http://127.0.0.1:57484/WebSite2/
下面是WCF功能代码

[WebInvoke(Method = "GET",RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]

        public List<string> getCurrentTime()
        {
            List<string> lstrSampleDate = new List<string>();
            lstrSampleDate.Add(DateTime.Now.TimeOfDay.ToString());
            return lstrSampleDate;
        }
当我执行代码时,控制台中会打印以下错误

作为一种解决方法,我在(网站的)配置文件中添加了以下设置


但仍然取得了丰硕的成果。有人能告诉我我失踪的地方吗

注意:我已将“localhost”替换为127.0.0.1
谢谢

也许你会发现这篇文章很有帮助:

我解决了访问控制允许源错误修改数据类型 数据类型参数:“jsonp”并添加跨域:true

 $.ajax({

                 type: "GET",
                 url: "http://127.0.0.1:54443/Service1.svc/getCurrentTime",
                 // crossDomain: true,
                 datatype: "json",
                 success: function (rVal) {
                     //console.log
                     alert(rVal);
                 },
                 error: function (xhr, status) {
                     //console.log
                     alert('Error: ' + xhr.statusText + 'Status: ' + xhr.status);
                 },
                 complete: function (xhr, status) {
                     //console.log
                     alert("The request is completed!");
                 }
             });
> XMLHttpRequest cannot load
> http://127.0.0.1:54443/Service1.svc/getCurrentTime. Origin
> http://127.0.0.1:57484 is not allowed by Access-Control-Allow-Origin.
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
<httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
</httpProtocol>
</system.webServer>