WCF call with AJAX

0

I need to make the ajax call on an html page that will trigger a WCF application

WCF Contract

[ServiceContract]
public interface IMailingService
{
    [OperationContract]
    ServiceRetorno<string> ValidarParceiro(string login, string senha);
}

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ControleMailingService : IMailingService
    {
        public ServiceRetorno<string> ValidarParceiro(string login, string senha)
        {

            ServiceRetorno<string> ret = new ServiceRetorno<string>();
            try
            {
                using (MailingData data = new MailingData())
                {
                    ret.Sucesso = data.ValidarParceiro(login, senha);
                    if (!ret.Sucesso)
                        ret.Mensagem = "Usuário ou senha inválido";
                    else
                        ret.DadosRetorno = CriarToken(login);
                }
            }
            catch(Exception ex)
            {
                ret.Sucesso = false;
                ret.Mensagem = "Falha ao validar login: " + ex.Message;
            }
            return ret;
        }
}

ENDPOINT CONFIGURATION

      <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="MailingService_Endpoint" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="WcfVisualFix.MailingService">
        <endpoint binding="basicHttpBinding" bindingConfiguration="MailingService_Endpoint"
          name="MailingService_Endpoint" contract="WcfVisualFix.IMailingService" />
        <endpoint binding="webHttpBinding" address="" 
          behaviorConfiguration="MailingServiceAjaxBehavior" contract="WcfVisualFix.IMailingService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="MailingServiceAjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false"  minFreeMemoryPercentageToActivateService="0" />

  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>

AJAX CALL

$(document).ready(function(){

$("#btnAcessar").click(function(){

 $.ajax({
type: "POST",
url: "http://visualfix.dyndns-web.com:93/MailingService.svc/ValidarParceiro",
data: { "login" : $("#txtUsuario").val(), "senha": $("#txtSenha").val() },
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: 
 function (res) {
if(res.Sucesso)
 $("#txtToken").val(res.DadosRetorno);
else
 $("#txtToken").val(res.Mensagem);
 },
error: 
 function (err) {
alert(err.responseJSON);
 }
 });

});
});

The call returns error statusCode 0.

XMLHttpRequest can not load link . Response to preflight request does not pass access control check: In 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 400.

Testing through SOAP UI works correctly.

    
asked by anonymous 30.11.2016 / 23:05

1 answer

0

This question you can configure in web config the accesses that the heade of the service will have, in JS really it will bugar because you probably did not specify anything in the tag in web config.

Take a look at this post that you will understand.

link

and explaining a little more: link

    
01.12.2016 / 05:55