[英]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.