如何从非Symfony2站点向Symfony2提交表单

时间:2014-08-27 12:30:14

标签: php ajax forms symfony

我正在构建一个移动网络应用程序,该应用程序从我构建的Symfony2应用程序中获取信息。用户必须提交一个表单才能查看信息,但是当我通过AJAX提交表单时,处理它的控制器没有看到提交的表单。

移动应用程序是使用Cordova构建的,因此表单源自静态HTML表单。一旦通过AJAX提交,然后将返回所需的数据。但是,表单提交了精确的触发AJAX以实现它的魔力,但Symfony2没有看到正在提交的表单。我在var_dump上做了$request = $this->getRequest();,此处不包含该表单。这是转储:

  

object(Symfony \ Component \ HttpFoundation \ Request)#6(20){   ["属性"] =>   object(Symfony \ Component \ HttpFoundation \ ParameterBag)#9(1){   ["参数":保护] => array(3){[" _controller"] =>串(60)   " CWWA \ CoreBundle \控制器\ AjaxController :: getscheduleAction"   [" _route"] => string(17)" ajax_get_schedule" [" _route_params"] =>   array(0){}}} [" request"] =>   object(Symfony \ Component \ HttpFoundation \ ParameterBag)#7(1){   ["参数":保护] => array(0){}} [" query"] =>   object(Symfony \ Component \ HttpFoundation \ ParameterBag)#8(1){   ["参数":保护] => array(0){}} [" server"] =>   object(Symfony \ Component \ HttpFoundation \ ServerBag)#12(1){   ["参数":保护] => array(35){[" BASE"] =>串(9)   " / CWWA /卷筒纸" [" CONTENT_LENGTH"] =>字符串(2)" 70" [" CONTENT_TYPE"] =>   string(31)" application / json;字符集= UTF-8" [" DOCUMENT_ROOT"] =>   string(8)" / var / www" [" GATEWAY_INTERFACE"] => string(7)" CGI / 1.1"   [" HTTP_ACCEPT"] => string(46)" application / json,text / javascript, / ;   Q = 0.01" [" HTTP_ACCEPT_ENCODING"] => string(13)" gzip,deflate"   [" HTTP_ACCEPT_LANGUAGE"] => string(14)" en-gb,en; q = 0.5"   [" HTTP_CACHE_CONTROL"] => string(8)" no-cache" [" HTTP_CONNECTION"] =>   string(10)" keep-alive" [" HTTP_COOKIE"] =>串(36)   " PHPSESSID = lvfqh6hcdampcah8mi93il6922" [" HTTP_HOST"] =>串(9)   " domain.com" [" HTTP_PRAGMA"] => string(8)" no-cache" [" HTTP_REFERER"] =>   字符串(26)" http://domain.com/cwwa-app/" [" HTTP_USER_AGENT"] =>   string(85)" Mozilla / 5.0(X11; Linux x86_64; rv:24.0)Gecko / 20140722   Firefox / 24.0 Iceweasel / 24.7.0" [" PATH"] => string(13)" / bin:/ usr / bin"   [" PATH_INFO"] => string(22)" / ajax / client / schedule /"   [" PATH_TRANSLATED"] => string(29)" /var/www/cwwa/web/app_dev.php"   [" QUERY_STRING"] => string(0)"" [" REDIRECT_STATUS"] => string(3)" 200"   [" REMOTE_ADDR"] => string(3)" :: 1" [" REMOTE_PORT"] => string(5)" 33568"   [" REQUEST_METHOD"] => string(4)" POST" [" REQUEST_URI"] =>串(43)   " /cwwa/web/app_dev.php/ajax/client/schedule /" [" SCRIPT_FILENAME"] =>   string(29)" /var/www/cwwa/web/app_dev.php" [" SCRIPT_NAME"] =>   string(21)" /cwwa/web/app_dev.php" [" SERVER_ADDR"] => string(3)" :: 1"   [" SERVER_ADMIN"] => string(19)" webmaster @ localhost" [" SERVER_NAME"] =>   string(9)" localhost" [" SERVER_PORT"] =>字符串(2)" 80"   [" SERVER_PROTOCOL"] => string(8)" HTTP / 1.1" [" SERVER_SIGNATURE"] =>   字符串(70)" Apache / 2.2.22(Debian)服务器在localhost端口80"   [" SERVER_SOFTWARE"] => string(22)" Apache / 2.2.22(Debian)"   [" PHP_SELF"] =>串(43)   " /cwwa/web/app_dev.php/ajax/client/schedule /" [" REQUEST_TIME"] =>   int(1409142446)}} [" files"] =>   object(Symfony \ Component \ HttpFoundation \ FileBag)#11(1){   ["参数":保护] => array(0){}} [" cookies"] =>   object(Symfony \ Component \ HttpFoundation \ ParameterBag)#10(1){   ["参数":保护] => array(1){[" PHPSESSID"] =>串(26)   " lvfqh6hcdampcah8mi93il6922" } {"标题"] =>   object(Symfony \ Component \ HttpFoundation \ HeaderBag)#13(2){   ["头":保护] => array(13){[" content-length"] => array(1){   [0] =>字符串(2)" 70" } [" content-type"] => array(1){[0] =>串(31)   "应用/ JSON;字符集= UTF-8" } ["接受"] => array(1){[0] =>   string(46)" application / json,text / javascript, / ; Q = 0.01" }   ["接受编码"] => array(1){[0] => string(13)" gzip,deflate" }   ["接受语言"] => array(1){[0] => string(14)" en-gb,en; q = 0.5" }   ["缓存控制"] => array(1){[0] => string(8)" no-cache" }   ["连接"] => array(1){[0] => string(10)" keep-alive" }   ["饼干"] => array(1){[0] =>串(36)   " PHPSESSID = lvfqh6hcdampcah8mi93il6922" } [" host"] => array(1){[0] =>   string(9)" localhost" } [" pragma"] => array(1){[0] =>串(8)   "无缓存" } [" referer"] => array(1){[0] =>串(26)   " http://domain.com/cwwa-app/" } [" user-agent"] => array(1){[0] =>   string(85)" Mozilla / 5.0(X11; Linux x86_64; rv:24.0)Gecko / 20140722   Firefox / 24.0 Iceweasel / 24.7.0" } [" x-php-ob-level"] => array(1){[0] =>   int(1)}} [" cacheControl":protected] => array(1){[" no-cache"] =>   bool(true)}} [" content":protected] => NULL [" languages":protected] =>   NULL [" charsets":protected] =>空值   [" acceptableContentTypes":保护] => NULL [" pathInfo":protected] =>   string(22)" / ajax / client / schedule /" [" requestUri":保护] =>   string(43)" /cwwa/web/app_dev.php/ajax/client/schedule /"   ["的baseUrl":保护] => string(21)" /cwwa/web/app_dev.php"   ["基本路径":保护] => NULL ["方法":protected] => string(4)" POST"   ["格式":保护] => NULL [" session":protected] =>   object(Symfony \ Component \ HttpFoundation \ Session \ Session)#67(3){   ["存储":保护] =>   对象(Symfony的\元器件\ HttpFoundation \会话\存储\ NativeSessionStorage)#66   (5){[" bags":protected] => array(2){[" attributes"] =>   对象(Symfony的\元器件\ HttpFoundation \会话\属性\ AttributeBag)#62   (3){   ["名称":" Symfony的\元器件\ HttpFoundation \会话\属性\ AttributeBag":私人] =>   string(10)" attributes"   [" storageKey":" Symfony的\元器件\ HttpFoundation \会话\属性\ AttributeBag":私人] =>   string(15)" _sf2_attributes" ["属性":保护] => array(0){}}   ["闪烁"] =>   object(Symfony \ Component \ HttpFoundation \ Session \ Flash \ FlashBag)#61(3)   {   ["名称":" Symfony的\元器件\ HttpFoundation \会话\闪存\ FlashBag":私人] =>   字符串(7)"闪烁"   ["闪烁":" Symfony的\元器件\ HttpFoundation \会话\闪存\ FlashBag":私人] =>   数组(0){}   [" storageKey":" Symfony的\元器件\ HttpFoundation \会话\闪存\ FlashBag":私人] =>   string(12)" _sf2_flashes" }"已启动":protected] =>布尔(假)   ["关闭":保护] => bool(false)[" saveHandler":protected] =>   对象(Symfony的\元器件\ HttpFoundation \会话\存储\代理\ NativeProxy)#63   (3){["包装":受保护] => bool(false)[" active":protected] =>   bool(false)[" saveHandlerName":protected] => string(5)" files" }   [" metadataBag":保护] =>   对象(Symfony的\元器件\ HttpFoundation \会话\存储\ MetadataBag)#64   (4){   ["名称":" Symfony的\元器件\ HttpFoundation \会话\存储\ MetadataBag":私人] =>   string(10)" __ metadata"   [" storageKey":" Symfony的\元器件\ HttpFoundation \会话\存储\ MetadataBag":私人] =>   string(9)" _sf2_meta" ["元":保护] => array(3){[" c"] => INT(0)   [" U"] => int(0)[" l"] => int(0)}   [" LASTUSED":" Symfony的\元器件\ HttpFoundation \会话\存储\ MetadataBag":私人] =>   空值 } }   [" flashName":" Symfony的\元器件\ HttpFoundation \会话\会话":私人] =>   字符串(7)"闪烁"   ["的attributeName":" Symfony的\元器件\ HttpFoundation \会话\会话":私人] =>   string(10)" attributes" } [" locale":protected] =>空值   [" defaultLocale":保护] => string(2)" en" }

HTML表单是这样的:

        <form method="post" name="scf" id="scheduleChangeForm" action="http://localhost/cwwa/web/app_dev.php/ajax/client/schedule/">
            <input type="hidden" id="scheduleChangeCustomer" class="form-control" name="scf[customer]" />
            <select id="divisionSelect" class="form-control" name="scf[division]">
                <option>All Divisions</option>
            </select>
            <select id="siteSelect" class="form-control siteDivSelect" name="scf[site]">
                <option>All Sites</option>
            </select>
            <button type="submit" class="btn btn-primary btn-go" id="chSBtn">Submit <i class="fa fa-arrow-circle-right"></i></button>
        </form>

AJAX就像这样:

var scf = $('#scheduleChangeForm');

scf.submit(function(){

    $.ajax({
        type: scf.attr('method'),
        url: scf.attr('action'),
        dataType: 'json',
        crossDomain: true,
        contentType: 'application/json',
        data: scf.serialize(),
        success: function(data) {
            // Deals with a successful AJAX submit
        }
    });
});

修改
我已将以下代码添加到控制器中:

$content = $this->get("request")->getContent();
print_r($content);

这返回了以下内容:

  

SCF%5Bcustomer%5D = 1&安培; SCF%5Bdivision%5D = CIRCOM +托管&安培; SCF%5Bsite%5D = 633

因此表单正在提交给请求,但不确定为什么它会像这样显示。

1 个答案:

答案 0 :(得分:0)

我认为你可能需要一个csrf令牌?