简体   繁体   English

Web Service C#ASP.NET无法发出请求

[英]Web Service C# ASP.NET cannot make requests

I'm in a proyect where I have to fill a textbox (as if it was a select control) with data requested from a Web Service while the user is writing in the textbox. 我在一个proyect中,当用户在文本框中书写时,我必须用Web服务请求的数据填充文本框(好像它是一个选择控件)。

The web service is developed in the same solution. Web服务是在相同的解决方案中开发的。

The problem is when I make a request from the client-side it throws me: 问题是,当我从客户端发出请求时,它抛出了我:

"Failed to load http://.../wsConsultaAfiliados.asmx: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I've searching for a while and I read that is a problem of CORS. 我搜索了一会儿,我读到这是CORS的问题。 However, I don't understand why. 但是,我不明白为什么。 I'm requesting the data to the web service from the client-side which are in the same domain. 我正在从同一域中的客户端向Web服务请求数据。

For what I searched I understand that CORS is for security reasons when you ask data from X domain to Y domain. 对于我搜索的内容,我了解到当您将数据从X域发送到Y域时,CORS是出于安全原因。

It's the first time I consume web service and I'm a bit lost about the scheme I should follow. 这是我第一次使用Web服务,而我对应该遵循的方案有些困惑。

Add code 添加代码

public class wsConsultaAfiliados : System.Web.Services.WebService
    {

        /// <summary>
        /// Busca una lista de Afiliados que coincidan con los parámetros de entrada. Devuele DataTable.
        /// </summary>
        /// <returns></returns>     
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        [WebMethod]
        public DataTable ConsultarAfiliados(string oPrefixText)
        {
            var listaAfiliados = GetAfiliados(oPrefixText);

            return listaAfiliados;
        }

        /// <summary>
        /// Devuelve un DataTable con Codigo y Nombre del afiliado.
        /// </summary>
        /// <param name="oPrefixText"></param>
        /// <returns></returns>
        public DataTable GetAfiliados(string oPrefixText)
        {
            negPrevision neg = new negPrevision();
            return neg.obtenerAfiliadosPorNombre(oPrefixText);
        }
    }

Here's the code from the web services. 这是来自Web服务的代码。 It ask for data to the logic layer. 它向逻辑层请求数据。

Update your web.config like this to allow every request 像这样更新您的web.config以允许每个请求

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
</system.webServer>

For IIS 7.5+ and Rewrite 2.0 you can use 对于IIS 7.5+和Rewrite 2.0,您可以使用

<system.webServer>
   <httpProtocol>
     <customHeaders>
         <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
         <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS,PUT,DELETE" />
     </customHeaders>
   </httpProtocol>
        <rewrite>            
            <outboundRules>
                <clear />                
                <rule name="AddCrossDomainHeader">
                    <match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
                        <add input="{HTTP_ORIGIN}" pattern="(http(s)?://((.+\.)?domain1\.com|(.+\.)?domain2\.com|(.+\.)?domain3\.com))" />
                    </conditions>
                    <action type="Rewrite" value="{C:0}" />
                </rule>           
            </outboundRules>
        </rewrite>
 </system.webServer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在asp.net Web应用程序(C#)中制作倒数计时器? - How to make a countdown timer in asp.net web application(C#)? 尝试使用C#asp.net Web表单使epaper动态化 - Trying to make epaper dynamic using C# asp.net web form ASP.NET C#Web窗体-如何在旧版应用程序中制作交互式CRUD页? - ASP.NET C# Web Forms - How to make an interactive CRUD page in a legacy application? 在 Asp.net C# Web 应用程序中集成 Bing 地图 - Integrating Bing Maps in Asp.net C# Web application 在ASP.NET C#中将网页上的内容另存为.doc - Saving the contents on a web page as .doc in asp.net c# 使用asp.net C#Javascript使用Finerprint登录到Asp.net Web应用程序 - Login into Asp.net Web application with Finerprint using asp.net C# Javascript 如何使用asp.net C#中的Ajax和Web服务使用数据库中的数据填充动态创建的下拉列表 - how fill the dynamically created dropdownlist with data from database using ajax and web service in asp.net c# ASP.NET Web服务中的Excel导出 - Excel Export in ASP.NET Web Service asp.net web 服务使用#angularjs - asp.net web service using #angularjs ASP.NET MVC 2数据验证:使C#正则表达式同时适用于C#和JavaScript? - ASP.NET MVC 2 Data Validation: Make C# Regex work for both C# and JavaScript simultaneously?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM