1

I'm working with BeSimpleSOAPBundle to do my web service on symfony2 . I test my web service with SOA Client extension firefox and I can see that my web service it's ok , but when Try to test with javascript this fails. I get the following mistakes from console browser

OPTIONS http://192.168.0.68/controlid/web/app_dev.php/ws/loginOperador?wdls 405  (Method Not Allowed) jquery.js:4
OPTIONS http://192.168.0.68/controlid/web/app_dev.php/ws/loginOperador?wdls No   'Access-Control-Allow-Origin' header is present on the requested resource. Origin   'http://localhost' is therefore not allowed access. jquery.js:4
XMLHttpRequest cannot load    http://192.168.0.68/controlid/web/app_dev.php/ws/loginOperador?wdls. No 'Access-Control-   Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is    therefore not allowed access. (index):1

The next code is the way to call my web service with javascript

function CallService()
{
    var webServiceURL = 'http://192.168.0.68/controlid/web/app_dev.php/ws/loginOperador?wdls';
    var soapMessage = '<?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><goodbye xmlns="http://controlid/ws/loginOperador/1.0/"><name>'+$('#nombre').val()+'</name></goodbye></soap:Body></soap:Envelope>';
   jQuery.support.cors = true;
   //Llamamos a la función AJAX de JQuery
   $.ajax({
      type: "POST",
      url: webServiceURL,
      crossDomain : true,
      cache: false,
      data: soapMessage,
      processData: false,
      dataType: "xml",
      contentType: "application/soap+xml; charset=utf-8",
      success: OnSuccess,
      error: OnError
   });
   return false;
}

This is the code of my web services on symfony:

/**
  * Método que se encarga de verificar si un operador
  * puede tener acceso desde la aplicación móvil.
  *
  * @Soap\Method("login")
  * @Soap\Param("pin", phpType = "string")
  * @Soap\Result(phpType = "string")
*/
public function loginAction( $pin )
{
    $em =  $this->container->get('doctrine')->getEntityManager();       
    try 
    {
        $operador = $em->getRepository('controlidRondasBundle:Operador')->findOneBy(array('pin'=>$pin));
        if( empty($operador) )
        {
            return sprintf('Acceso denegado');
        }
        else{
            return sprintf('Bienvenido %s!', $operador->getNombre() );
        }
    } 
    catch (\Doctrine\Orm\NoResultException $e) 
    {
        return sprintf('Acceso denegado');
    }
}

An this is the setup of my service on symfony ( app/conf/config.yml)

be_simple_soap: 
   services:
        DemoApi:
            namespace:     http://controlid/ws/DemoApi/1.0/
            binding:       rpc-literal
            resource:      "@controlidRondasBundle/Controller/RegisterController.php"
            resource_type: annotation
        loginOperador:
            namespace:     http://controlid/ws/loginOperador/1.0/
            binding:       rpc-literal
            resource:      "@controlidSeguridadBundle/Controller/OperatorSecurityController.php"
            resource_type: annotation

And routing_dev.yml:

_besimple_soap:
    resource: "@BeSimpleSoapBundle/Resources/config/routing/webservicecontroller.xml"
    prefix:   /ws

Someone could say me what do I'm doing wrong please?

2
  • I can't see anywhere in your code that is setting the 'Access-Control-Allow-Origin' header, which the message makes clear is required. Also, why on earth would you want to mix SOAP and JS? (Sorry, but I find SOAP a needlessly complex way of failing to achieve interoperability between environments.) Commented May 12, 2014 at 22:19
  • 1. how to setup the header with Access-Control-Allow-origin? 2. SOAP + JS because The web service is consumed by a phonegap application Commented May 12, 2014 at 22:33

1 Answer 1

3

The thing that I had to do is setup my apache server

  1. Activate the module
a2enmod headers
service apache2 restart

2 . And edit the file /etc/apache2/sites-enabled

<VirtualHost *:80>
 ....

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

...

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

...
    #The things that I added 

Header set Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, content-type"
Header always set Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"

    #End of the thigns that I added 

</VirtualHost>

EDIT

The above answer only works on localhost, but when I tryed to do it on hosting or whaterver cloud, The mistake persevere, so the answer was the magic bundle

The installation :

php composer.phar require nelmio/cors-bundle:~1.0

Then add to project/app/AppKernel.php

public function registerBundles()
{
   $bundles = array(
    ...
    new Nelmio\CorsBundle\NelmioCorsBundle(),
    ...
   );
   ...
}

Now the setup of the bundle and my paths on project/app/config/config.yml add

nelmio_cors:
   defaults:
       allow_credentials: true
       allow_origin: []
       allow_headers: []
       allow_methods: []
       expose_headers: []
       max_age: 0
       hosts: []
   paths:
    '^/':
        allow_origin: ['*']
        allow_headers: ['origin', 'content-type']
        allow_methods: ['POST', 'PUT', 'GET', 'DELETE','OPTIONS']
        max_age: 3600

To summary it's works!!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.