Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.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
Javascript 设置访问控制允许原点_Javascript_.net_Ajax_Web Services - Fatal编程技术网

Javascript 设置访问控制允许原点

Javascript 设置访问控制允许原点,javascript,.net,ajax,web-services,Javascript,.net,Ajax,Web Services,我有一个简单的.NET web服务: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; /// <summary> /// Summary description for WebService /// </summary> [WebService(Namespace = "http://tempur

我有一个简单的.NET web服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    public int Add(int x, int y)
    {
        return x+y;
    }


}
据我所知,这是某种CORS问题。不幸的是,我找不到解决办法。我到底要做什么。在哪里以及如何解决问题-在服务器、客户端甚至浏览器中

UPD: 正在更新web.config。现在看来:

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
</configuration>

但现在出现了内部服务器错误500:

Remote Address:[::1]:41026
Request URL:http://localhost:41026/WebService.asmx
Request Method:POST
Status Code:500 Internal Server Error
Response Headers
view source
Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Cache-Control:private
Content-Length:1749
Content-Type:text/xml; charset=utf-8
Date:Tue, 03 Nov 2015 15:29:32 GMT
Server:Microsoft-IIS/8.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?QzpcanNjcmlwdG5vbnNlbnNcV2ViU2l0ZTJcV2ViU2VydmljZS5hc214?=
Request Headers
view source
Accept:application/xml, text/xml, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:337
Content-Type:text/xml
Host:localhost:41026
Origin:null
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36
Request Payload
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">   <soap:Body>     <Add xmlns="http://tempuri.org/">       <x>int</x>       <y>int</y>     </Add>   </soap:Body> </soap:Envelope>
远程地址:[::1]:41026
请求URL:http://localhost:41026/WebService.asmx
申请方式:邮寄
状态代码:500内部服务器错误
响应头
视图源
访问控制允许标头:内容类型
访问控制允许方法:获取、发布、放置、删除、选项
访问控制允许来源:*
缓存控制:专用
内容长度:1749
内容类型:text/xml;字符集=utf-8
日期:2015年11月3日星期二15:29:32 GMT
服务器:Microsoft IIS/8.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?QzpcanNjcmlwdG5vbnNlbnNcV2ViU2l0ZTJcV2ViU2VydmljZS5hc214=
请求头
视图源
接受:application/xml,text/xml,*/*;q=0.01
接受编码:gzip,deflate
接受语言:en-US,en;q=0.8
连接:保持活力
内容长度:337
内容类型:text/xml
主机:localhost:41026
来源:空
用户代理:Mozilla/5.0(Windows NT 6.3;WOW64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/46.0.2490.80 Safari/537.36
请求有效载荷
整型

您需要允许web.config中的自定义标题,即

<system.webServer>
 <httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>
 </httpProtocol>
</system.webServer>


我已根据您的建议更新了配置,但现在出现了服务器错误500。正文中的详细信息有疑问。上述代码需要放在web.config文件的configuration元素下。我认为这是一个语法错误导致的500。你能发布上面显示复制的xml的web.config内容吗(确保首先去掉敏感值:))对不起,没有看到你的编辑(在手机上,错过了它),忽略我的上一条评论OK,这需要一点认识。如果您遇到500服务器错误,则CORS请求被拒绝的原始问题已得到解决。你现在经历的500是另一回事。是的,你的答案解决了CORS。我通过修复请求体中的int\int\解决了500问题。
Remote Address:[::1]:41026
Request URL:http://localhost:41026/WebService.asmx
Request Method:POST
Status Code:500 Internal Server Error
Response Headers
view source
Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Cache-Control:private
Content-Length:1749
Content-Type:text/xml; charset=utf-8
Date:Tue, 03 Nov 2015 15:29:32 GMT
Server:Microsoft-IIS/8.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?QzpcanNjcmlwdG5vbnNlbnNcV2ViU2l0ZTJcV2ViU2VydmljZS5hc214?=
Request Headers
view source
Accept:application/xml, text/xml, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:337
Content-Type:text/xml
Host:localhost:41026
Origin:null
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36
Request Payload
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">   <soap:Body>     <Add xmlns="http://tempuri.org/">       <x>int</x>       <y>int</y>     </Add>   </soap:Body> </soap:Envelope>
<system.webServer>
 <httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>
 </httpProtocol>
</system.webServer>