CORS on Symfony 2错误BeSimpleSOAPBundle使用Javascript调用WebService [英] CORS on Symfony 2 errors BeSimpleSOAPBundle calling WebService with Javascript
问题描述
我使用 BeSimpleSOAPBundle 在symfony2上执行我的网络服务。我使用 SOA客户端扩展firefox 和我测试我的网络服务可以看到我的web服务它的确定,但当尝试使用javascript测试时,这将失败。我从控制台浏览器收到以下错误
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
下一个代码是使用javascript调用我的Web服务的方法
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;
}
这是symfony上我的web服务的代码:
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');
}
}
这是我在symfony app / conf / config.yml)
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
和routing_dev.yml:
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?
推荐答案
我需要做的是设置我的 apache服务器
The thing that I had to do is setup my apache server
a2enmod headers
service apache2 restart
2。并编辑文件 / 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
上面的答案只适用于localhost,但是当我尝试在托管或云端做时,错误持续,所以答案是魔法包
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
安装:
php composer.phar require nelmio/cors-bundle:~1.0
c $ c> project / app / AppKernel.php
Then add to project/app/AppKernel.php
public function registerBundles()
{
$bundles = array(
...
new Nelmio\CorsBundle\NelmioCorsBundle(),
...
);
...
}
现在设置包和我的路径 project / app / config / config.yml
添加
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
汇总它的作品!
这篇关于CORS on Symfony 2错误BeSimpleSOAPBundle使用Javascript调用WebService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!